0

我怎样才能将箭头的图像移动到 2004 条上?提前致谢!

在此处输入图像描述 这是我的代码:

data %>% 
  ggplot(aes(x=year, y=value, fill = gender)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values= c('#1380c9', '#ff6262')) +
  scale_y_continuous(limits=c(0, 1000)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=8))+
  annotation_custom(rasterGrob(image, interpolate=TRUE, height = 0.08, width = 0.08))
4

1 回答 1

1

您可以使用 、 、 和 中的参数指定图像xminxmax位置yminymax大小annotation_custom。只需保留默认大小设置rasterGrob

library(ggplot2)
library(grid)

data %>%
  ggplot(aes(x=year, y=value, fill = gender)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values= c('#1380c9', '#ff6262')) +
  scale_y_continuous(limits=c(0, 1000)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=8)) +
  annotation_custom(rasterGrob(image, interpolate=TRUE),
                    xmin = 3.6, xmax = 5.5, ymin = 700, ymax= 850)

当然,我没有您的数据或相同的箭头图形,但这给了我:

在此处输入图像描述

这是我使用的数据:

data <- structure(list(year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L), class = "factor", .Label = c("2000", "2001", "2002", 
"2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", 
"2011", "2012", "2013", "2014", "2015", "2016", "2017")), value = c(124.941847570307, 
157.345732968883, 116.574855503598, 213.811232085512, 260, 117.181264635279, 
169.497162097139, 179.532988205169, 173.03125406614, 137.784464513746, 
210.471246738034, 165.593729456457, 125.150376778328, 61.4120045129, 
194.997236725724, 148.202655639391, 149.352389476042, 187.753448427412, 
324.636635852943, 317.817039636525, 327.569321148247, 323.464089021932, 
500, 240.319449124099, 318.594772436841, 298.31613781413, 295.32613479884, 
255.877428483022, 285.655498346741, 312.538246805991, 340.760386545871, 
296.91636817971, 311.630148346781, 298.385848782513, 258.688213295142, 
287.55016310101), gender = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Hombres", 
"Mujeres"), class = "factor")), row.names = c(NA, -36L), class = "data.frame")

以使这个可重现的名义,我在我的主目录中保存了一个箭头图像并做了

library(png)
image <- readPNG(path.expand("~/arrow.png"))

但如果有人想测试一下,这里有一张低分辨率的测试图像:

arrow <- c(22L, 23L, 24L, 42L, 43L, 44L, 52L, 62L, 63L, 64L, 72L, 73L,
82L, 83L, 84L, 92L, 93L, 94L, 102L, 103L, 104L, 113L, 114L, 115L,
122L, 123L, 124L, 125L, 133L, 134L, 135L, 136L, 142L, 143L, 144L,
145L, 153L, 154L, 155L, 156L, 157L, 163L, 164L, 165L, 166L, 167L,
168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L,
183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 198L, 199L, 204L, 205L, 206L, 207L, 208L,
209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L,
225L, 226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L,
236L, 237L, 238L, 253L, 254L, 255L, 256L, 257L, 273L, 274L, 275L,
276L, 293L, 294L, 295L, 312L, 313L, 314L, 332L, 333L, 352L)

image <- array(rep(0, 20*19*4), dim=c(20,19,4))
image[,,4][arrow] <- 1
于 2020-01-17T20:07:11.237 回答