r - 修改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$")
推荐阅读
- vue.js - 为什么在 IIS 上启用 vue.js 历史模式时,文件下载会重定向到主页
- python-3.x - 如何使用python比较从行中间的每一行与单个文本文件中的每一行?
- python-3.x - Python: subprocess.call('nvm ls', shell=True) 给出这个错误 /bin/sh: nvm: command not found
- list - 如何将数据数组转换为在颤振/飞镖中展开或折叠的小部件列表?
- visual-studio-2017 - 从 Visual Studio 2017 Pro 迁移到社区版
- webstorm - 无法将 WSL 节点解释器添加到 WebStorm
- javascript - .push() 覆盖 Javascript 中的数组值
- sql - 最小和最大游戏数之间的差异
- julia - 如何在 Julia 中限制数组的列
- javascript - 我的自动段落幻灯片不起作用