gpt4 book ai didi

r - ggplot 形状与图例不匹配

转载 作者:行者123 更新时间:2023-12-03 23:34:01 25 4
gpt4 key购买 nike

希望手动添加图例(在这种情况下,我不想让数据框变长)但图例形状与图表不匹配:

library(ggplot2)
mtcars$time <- 1:nrow(mtcars)
ggplot(mtcars) +

#wt
geom_line(aes(x = time, y = wt, color = "wt name"), size = 1.5) +
geom_point(aes(x = time, y = wt, color = "wt name"), shape = 19, size = 4) +

#drat
geom_line(aes(x = time, y = drat , color = "drat name"), size = 1.5) +
geom_point(aes(x = time, y = drat , color = "drat name"), shape = 8, size = 4) +

scale_colour_manual(name = "legend", values = c("wt name" = "#F8766D",
"drat name" = "#B79F00"))

结果:

enter image description here

其他问题建议使用 scale_shape_manual 但我无法让它工作:

ggplot(mtcars) +

#wt
geom_line(aes(x = time, y = wt, color = "wt name"), size = 1.5) +
geom_point(aes(x = time, y = wt, color = "wt name"), size = 4) +

#drat
geom_line(aes(x = time, y = drat , color = "drat name"), size = 1.5) +
geom_point(aes(x = time, y = drat , color = "drat name"), size = 4) +

scale_colour_manual(name = "legend", values = c("wt name" = "#F8766D",
"drat name" = "#B79F00")) +
scale_shape_manual(values = c("wt name" = 19,
"drat name" = 8 ))

给出这个忽略形状:

enter image description here

有谁知道这个简单的问题需要改变什么吗?谢谢

最佳答案

如果您希望有一个自定义图例而不是 reshape 您的数据(这将是使用 ggplot 的标准方法),您需要将形状映射为美学(即把 shape = 放在里面aes 调用,并给它你希望出现在图例上的名称)。如果您希望将它们合并到图例中,您还需要确保两个比例具有相同的 name = :

library(ggplot2)

mtcars$time <- 1:nrow(mtcars)

ggplot(mtcars, aes(x = time)) +
geom_line(aes(y = wt, color = "wt name"), size = 1.5) +
geom_point(aes(y = wt, color = "wt name", shape = "wt name"), size = 4) +
geom_line(aes(y = drat, color = "drat name"), size = 1.5) +
geom_point(aes(y = drat, color = "drat name", shape ="drat name"), size = 4) +
scale_colour_manual(values = c("wt name" = "#F8766D", "drat name" = "#B79F00"),
name = "legend") +
scale_shape_manual(values = c("wt name" = 19, "drat name" = 8 ),
name = "legend")

enter image description here

或者如果你想将它们分开,给它们起不同的名字:

library(ggplot2)

mtcars$time <- 1:nrow(mtcars)

ggplot(mtcars, aes(x = time)) +
geom_line(aes(y = wt, color = "wt name"), size = 1.5) +
geom_point(aes(y = wt, color = "wt name", shape = "wt name"), size = 4) +
geom_line(aes(y = drat, color = "drat name"), size = 1.5) +
geom_point(aes(y = drat, color = "drat name", shape ="drat name"), size = 4) +
scale_colour_manual(values = c("wt name" = "#F8766D", "drat name" = "#B79F00"),
name = "legend2") +
scale_shape_manual(values = c("wt name" = 19, "drat name" = 8 ),
name = "legend1")

并且,根据评论中的要求,如果您想要两个图例,但每个图例中只有一个颜色和形状,您将需要破解 breaksguide ,因为这不是美学映射的标准用法。

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.6.3

mtcars$time <- 1:nrow(mtcars)

ggplot(mtcars, aes(x = time)) +
geom_line(aes(y = wt, color = "wt name"), size = 1.5) +
geom_point(aes(y = wt, color = "wt name", shape = "wt name"), size = 4) +
geom_line(aes(y = drat, color = "drat name", size = "wt name")) +
geom_point(aes(y = drat, color = "drat name", shape ="drat name"), size = 4) +
scale_size_manual(values = c("wt name" = 1.5), breaks = "wt name",
name = "legend2") +
scale_colour_manual(values = c("wt name" = "#F8766D", "drat name" = "#B79F00"),
name = "legend1", breaks = "drat name") +
scale_shape_manual(values = c("wt name" = 19, "drat name" = 8 ),
name = "legend2", breaks = "wt name") +
guides(colour = guide_legend(override.aes = list(shape = 8, color = "#B79F00")),
shape = guide_legend(override.aes = list(shape = 19, color = "#F8766D")))

reprex package 于 2020-09-03 创建(v0.3.0)

关于r - ggplot 形状与图例不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63722900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com