我想在ggplot2的同一图表上绘制点(datpoint
)和拟合曲线(datline
),我需要查看我的点和拟合曲线的图例。不幸的是,ggplot2只绘制点的图例而不是线条。我尝试了不同的ggplot选项,如scale_linetype_manual
,scale_colour_manual
和其他选项,但结果不是预期的。美学论点似乎很难使用。
library(ggplot2)
datpoint <- structure(list(conc = c(0, 0.074, 0.22, 0.66, 0, 0.074, 0.22, 0.66, 0, 0.074, 0.22, 0.66),
resp = c(2.9324, 3.1687, 2.6380, 0.5360, 3.3622, 3.2655, 2.7124, 0.0905, 3.1875, 3.0549, 2.7410, 0.1085),
mortality = c(1, 1, 1, 1, 19, 1, 1, 19, 1, 1, 19, 1)),
.Names = c("conc", "resp", "mortality"), row.names = c(NA, -12L), class = "data.frame")
datline <- structure(list(X = c(0, 0.06, 0.1266, 0.1933, 0.26, 0.3266, 0.3933, 0.46, 0.5266, 0.5933, 0.66), Y = c(3.1470, 3.1463, 3.1241, 2.9947, 2.6272, 2.013, 1.3566, 0.8493, 0.5218, 0.3248, 0.2076)), .Names = c("X", "Y"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), class = "data.frame")
fitcol = "red"
fitlty = 1
fitlwd = 1
legend.title = "Mortality"
fg <- ggplot(datpoint, aes(conc, resp, color = factor(mortality))) +
geom_point(size = 2.5) +
geom_line(aes(X,Y), datline , color = fitcol, linetype = fitlty, size = fitlwd) +
scale_colour_hue(legend.title, breaks = c("1","19"), labels = c("dead", "alive")) +
labs(x = "conc", y = "resp")
fg
任何帮助绘制点的图例和具有正确标题(Fitted curve
)的拟合曲线都将非常感激。谢谢!
答案 0 :(得分:1)
您需要在aes
中映射颜色,以使其显示在图例中:
fg <- ggplot(datpoint, aes(conc, resp, color = factor(mortality))) +
geom_point(size = 2.5) +
geom_line(aes(X,Y, color = "fit"), datline ,
linetype = fitlty, size = fitlwd) +
scale_colour_hue(legend.title, breaks = c("1","19", "fit"),
labels = c("dead", "alive", "fit")) +
labs(x = "conc", y = "resp")
fg