0

我有这个图形,我想在水平轴上的年份下方添加一个图例。我尝试了几个选项,但我看不到一个图例。我会很感激你的帮助。谢谢。

ggplot(AIA) +
  geom_segment( aes(x=X, xend=X, y=A, yend=B), color="black") +
  geom_point( aes(x=X, y=A), color= "orange", size=4 ) +
  geom_point( aes(x=X, y=B), color= "deepskyblue4", size=4 ) +
  coord_flip()+
  theme_light() +
  theme(
    legend.position = c (0.95),
    panel.border = element_blank(),) +
  xlab("Innovaciones") +
  ylab("Años")

数据示例

structure(list(X = c("Var. locales", "Var. Comerciales", "Comestibles", 
"Silvestres", "Fert. y abonos", "Análisis del suelo", "Manejo suelo", 
"Veg. Espontanea", "Métodos de siempra", "Plagas y enfermedades", 
"Desinfección", "Setos", "Refugios cajas nido", "Integración animal (otra)", 
"Composteras", "Maquinaria", "Eficiencia energética", "Riego", 
"Software y equipos", "Distribución"), A = c("1990", "1990", 
"1995", "1995", "1995", "1995", "2008", "1995", "1996", "2012", 
"2006", "1998", "2011", "1988", "1996", "1997", "1997", "1997", 
"2000", "2012"), B = c("2018", "2012", "2013", "1995", "2018", 
"2018", "2013", "2015", "2015", "2015", "2018", "2013", "2018", 
"2018", "1996", "2016", "1997", "2012", "2016", "2012")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

在此处输入图像描述

4

1 回答 1

0

如果您不想将数据重塑为长格式,您可以colour =在调用中使用每个组名aes并添加一个scale_color_manual. 要将图例放在情节下方,请使用theme(legend.position = "bottom")

ggplot(AIA) +
  geom_segment(aes(x = X, xend = X, y = A, yend = B)) +
  geom_point(aes(x = X, y = A, color = "A"), size = 4) +
  geom_point(aes(x = X, y = B, color = "B"), size = 4) +
  scale_color_manual(values = c("orange", "deepskyblue4"),
                     guide  = guide_legend(), 
                     name   = "Group") +
  xlab("Innovaciones") +
  ylab("Años")  +
  coord_flip()  +
  theme_light() +
  theme(legend.position = "bottom",
        panel.border    = element_blank())

在此处输入图像描述

于 2020-07-29T12:07:59.623 回答