我是一个绝对的初学者,自从我开始使用以来已经有2-3天了ggplot2.到目前为止,我一直使用Excel进行图形处理.ggplot2真的杀了我,所以我想在这里发布我的查询.
昨晚,我讨论了如何geom_smooth()用另一层进行绘图,比如geom_point()这里讨论的内容:绘图中使用的美学尺度 GGPLOT2
继续这一点,我想到尝试多个geom_smooth().
这是我做的:
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
geom_smooth(method = "loess", se = FALSE, color = "black", aes(linetype = "loes")) +
geom_smooth( method = "lm", se = FALSE, color = "red", aes(linetype = "lm",color = "green")) +
labs(colour = "Method")
Run Code Online (Sandbox Code Playgroud)
除了我添加了另一个之外,它与前一个代码类似 geom_smooth().
输出是:
我还看了多层ggplot2的格式图例似乎我可以手动覆盖颜色.
我们可以看到,第三层仍然覆盖第二层的颜色(在图例中).
所以,这就是我做的:
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
geom_smooth(method = "loess", se = FALSE, color = "123", aes(linetype = "loes")) +
geom_smooth( method = "lm", se = FALSE, color = "345", aes(linetype = "lm",color = "green")) +
scale_colour_manual(values=c("coral", "chocolate", "cornsilk", "papayawhip", "blanchedalmond","red","black","yellow","pink")) +
labs(colour = "Method")
Run Code Online (Sandbox Code Playgroud)
第三层仍然覆盖第二层的颜色(在图例中).我很感激你的帮助.
我有两个问题:
问题1:我上面发布的问题是否有任何解决方法?我很感激任何想法.有没有解决这个问题?我很感激任何想法.
问题2:我注意到有时候人们会使用aes(linetype = "lm"),有时他们只是(linetype = "lm")在里面使用geom_smooth().我们为什么要做这个?我相信如果我们使用aes(..)我在这里没有明确的假设,所以我会避免猜测.我很感激你的想法.
更新:我的问题是关于发布的解决方案.
我们可以不使用任何其他形状的散点图吗?已发布的解决方案建议将形状更改为大小= 21,这是我有点不舒服的事情.
我更改了代码(在下面的解决方案中)以获取其他形状,如下所示:
huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
ggplot(mpg, aes(displ, hwy)) +
# map geom_point class to 'fill'
geom_point(shape=5, aes(color = class)) +
# use color and linetype for geom_smooth
geom_smooth(method = "loess", se = FALSE,
aes(linetype = "loess", color = 'loess')) +
geom_smooth(method = "lm", se = FALSE,
aes(linetype = "lm", color = "lm")) +
# merge linetype and color legends by giving them the same name
scale_linetype_discrete(name = "Method") +
scale_color_manual(name = "Method", values = c("red", "black","coral", "chocolate", "cornsilk", "papayawhip", "blanchedalmond","red","black"))
Run Code Online (Sandbox Code Playgroud)
但是,运行此代码后,我们将看到lm和loess的颜色已重置为蓝色,散点图的图例不再是实体类型.我能够改变形状,但不能改变颜色问题和图例问题.有什么想法吗?
使用fill和空心形状的geom_point和colorgeom_smooth.
huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
ggplot(mpg, aes(displ, hwy)) +
# map geom_point class to 'fill'
geom_point(shape=21, aes(fill = class), color = NA) +
# use color and linetype for geom_smooth
geom_smooth(method = "loess", se = FALSE,
aes(linetype = "loess", color = 'loess')) +
geom_smooth(method = "lm", se = FALSE,
aes(linetype = "lm", color = "lm")) +
# merge linetype and color legends by giving them the same name
scale_linetype_discrete(name = "Method") +
scale_color_manual(name = "Method", values = c('red', 'black'))
Run Code Online (Sandbox Code Playgroud)
但是,我还要指出,如果您希望颜色信息用于区分点类,那么平滑线的不同颜色会让人分心.我认为最好将两条平滑的线条都留下黑色 - 线型足以区分它们
| 归档时间: |
|
| 查看次数: |
536 次 |
| 最近记录: |