簡體   English   中英

修改ggplot2中的圖例

[英]Modify legend in ggplot2

我想在下一個情節的圖例中添加虛線和黑色段: 陰謀

structure(list(Species = structure(c(2L, 4L, 1L, 5L, 3L, 6L), .Label = c("Lithobates catesbeianus",  "Eleutherodactylus coqui", "Rhinella marina", "Eleutherodactylus planirostris", "Polypedates leucomystax", "Xenopus laevis"), class = "factor", scores = structure(c(`Eleutherodactylus coqui` = -153579482.74, `Eleutherodactylus planirostris` = -4270489.16, `Lithobates catesbeianus` = -6040180271.32, `Polypedates leucomystax` = -169747.4, `Rhinella marina` = -76676305.87, `Xenopus laevis` = -75965.79), .Dim = 6L, .Dimnames = list(c("Eleutherodactylus coqui", "Eleutherodactylus planirostris", "Lithobates catesbeianus", "Polypedates leucomystax", "Rhinella marina", "Xenopus laevis")))), Costs = c(153579482.74, 4270489.16, 6040180271.32, 169747.4, 76676305.87, 75965.79), Million = c(153.5, 4.27, 6040.18, 0.17, 76.67, 0.075), Observed = c(18.29, 4.27, 3.95, 0.17, 43.52, 0.075)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")).

我正在使用的腳本是這樣的:

p1 <- ggplot(data = b,
        aes(Species, Million))   + ylab("Costs in Million US$")+xlab("Species") + scale_y_continuous(trans = "log10") + geom_segment(aes(xend=Species, yend=Million, y=0)) + theme_classic() +  geom_segment(aes(xend=Species,yend= Observed, y =0, colour="red"))  + theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1)) + geom_point( size=3, color="black") + geom_hline(yintercept=1045.81, linetype="dashed", color = "green") + geom_hline(yintercept= mean(b$Observed), linetype="dashed", color = "blue")+ scale_color_discrete(c("Costs Million US$"), labels= "Observed costs") 

我感謝所有的幫助

將線型和顏色帶入 aes 並為每個分配線型。

p1 <- ggplot(data = b,
             aes(Species, Million))   + 
  ylab("Costs in Million US$")+xlab("Species") + 
  scale_y_continuous(trans = "log10") + 
  geom_segment(aes(xend=Species, yend=Million, y=0, color="black", 
                   linetype = "black")) + 
  theme_classic() +  
  geom_segment(aes(xend=Species,yend= Observed, y =0, color="red",
                   linetype = "red"))  + 
  theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1)) + 
  geom_point(color="black", size=3, ) + 
  geom_hline(aes(color = "green", yintercept=1045.81, 
                 linetype = "green")) + 
  geom_hline(aes(color = "blue", yintercept = mean(Observed),
                 linetype="blue")) +
  scale_color_manual(values = c("red" = "red", "black" = "black", 
                                  "green" = "green", "blue" = "blue"),
                     labels = c("red" = "Observed costs", "black" = "black segment", 
                                "green" = "green line", "blue" = "blue line"),
                     "Costs Million US$") +
  scale_linetype_manual(values = c("red" = "solid", "black" = "solid", 
                                   "green" = "dashed", "blue" = "dashed"),
                        labels = c("red" = "Observed costs", "black" = "black segment", 
                                   "green" = "green line", "blue" = "blue line"),
                        "Costs Million US$")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM