R语言 不同大小的geom_segments产生怪异的图例

gcuhipw9  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(164)

我有一个geom_segments大小不同的图形。图例中的虚线和点大小不同。如何防止这种情况?
可复制的示例:

dataframe = data.frame(country = c("Burundi", "Kenya"),
                       rate1 = c(10, 3),
                       threshold1 = c(180, 72.2),
                       rate2 = c(25, 20),
                       threshold2 = c(242, 144),
                       rate3 = c(30,30))
xstart_limit <- 0
xend_limit <- 10000
linetypes <- c("dotted", "dashed")
my_colors <- c("blue","red")

pit_comparison2 <- ggplot() +
  geom_segment(aes(x = xstart_limit, y = rate1, xend = threshold1, yend = rate1, colour = country, linetype = country), size = 0.6, data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold1, y = rate1, xend = threshold1, yend = rate2, colour = country, linetype = country), size = 0.6, data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold1, y = rate2, xend = threshold2, yend = rate2, colour = country, linetype = country), size = 0.6, data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold2, y = rate2, xend = threshold2, yend = rate3, colour = country, linetype = country), size = 0.6, data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold2, y = rate3, xend = xend_limit, yend = rate3, colour = country, linetype = country), size = 0.6, data = dataframe[dataframe$country == "Kenya",]) +
  
  geom_segment(aes(x = xstart_limit, y = rate1, xend = threshold1, yend = rate1, colour = country, linetype = country), size = 1, data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold1, y = rate1, xend = threshold1, yend = rate2, colour = country, linetype = country), size = 1, data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold1, y = rate2, xend = threshold2, yend = rate2, colour = country, linetype = country), size = 1, data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold2, y = rate2, xend = threshold2, yend = rate3, colour = country, linetype = country), size = 1, data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold2, y = rate3, xend = xend_limit, yend = rate3, colour = country, linetype = country), size = 1, data = dataframe[dataframe$country == "Burundi",]) +
  
  scale_linetype_manual(values = linetypes, breaks=c("Burundi", "Kenya")) +
  scale_color_manual(values = my_colors, breaks=c("Burundi", "Kenya")) 

pit_comparison2

我尝试了许多不同的东西,但从来没有能解决这个问题的传说。

ekqde3dh

ekqde3dh1#

您可以将linewidth放在美学Map中,并将其Map到`country。

library(ggplot2)
dataframe = data.frame(country = c("Burundi", "Kenya"),
                       rate1 = c(10, 3),
                       threshold1 = c(180, 72.2),
                       rate2 = c(25, 20),
                       threshold2 = c(242, 144),
                       rate3 = c(30,30))
xstart_limit <- 0
xend_limit <- 10000
linetypes <- c("dotted", "dashed")
my_colors <- c("blue","red")

pit_comparison2 <- ggplot() +
  geom_segment(aes(x = xstart_limit, y = rate1, xend = threshold1, yend = rate1, colour = country, linetype = country, linewidth=country), , data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold1, y = rate1, xend = threshold1, yend = rate2, colour = country, linetype = country, linewidth=country), data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold1, y = rate2, xend = threshold2, yend = rate2, colour = country, linetype = country, linewidth=country), data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold2, y = rate2, xend = threshold2, yend = rate3, colour = country, linetype = country, linewidth=country), data = dataframe[dataframe$country == "Kenya",]) +
  geom_segment(aes(x = threshold2, y = rate3, xend = xend_limit, yend = rate3, colour = country, linetype = country, linewidth=country), data = dataframe[dataframe$country == "Kenya",]) +
  
  geom_segment(aes(x = xstart_limit, y = rate1, xend = threshold1, yend = rate1, colour = country, linetype = country, linewidth=country), data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold1, y = rate1, xend = threshold1, yend = rate2, colour = country, linetype = country, linewidth=country),  data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold1, y = rate2, xend = threshold2, yend = rate2, colour = country, linetype = country, linewidth=country),  data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold2, y = rate2, xend = threshold2, yend = rate3, colour = country, linetype = country, linewidth=country),  data = dataframe[dataframe$country == "Burundi",]) +
  geom_segment(aes(x = threshold2, y = rate3, xend = xend_limit, yend = rate3, colour = country, linetype = country, linewidth=country),  data = dataframe[dataframe$country == "Burundi",]) +
  scale_linewidth_manual(values=c(.6,1)) +   
  scale_linetype_manual(values = linetypes, breaks=c("Burundi", "Kenya")) +
  scale_color_manual(values = my_colors, breaks=c("Burundi", "Kenya")) 

pit_comparison2

创建于2023-04-09使用reprex v2.0.2

uurity8g

uurity8g2#

你可以通过在guides()中定义和覆盖这些美学来修复图例的外观 * 而不修改你在图中看到的内容 *。然而,我也会 * 强烈建议 * 一种不同的方法来绘制你这里的数据。

改变传说中的美学(不改变剧情)

guides()函数可用于指定显示每个图例的不同方式。由于您将colorlinetype美学应用于数据,我们必须注意不要以导致拆分图例的方式更改外观。您可以通过guide_legend(override.aes=list(...))为图例中的每个项目指定不同的linewidth值。I'我还指定了一个更宽的键宽度,以更好地显示线条。

pit_comparison2 + guides(
  linetype=guide_legend(
    override.aes = list(linewidth=c(1,0.6)),
    keywidth = unit(1, "cm")
  )
)

更好的绘图方式:使用路径几何体

更好的绘图方法是(1)使用tidy data principles更好地组织数据,以及(2)使用geom_path()而不是单独调用geom_segment()。如果数据变得很大,你将有一个庞大的代码集来创建你的绘图。而且,每当你得到新数据时,你必须把这些数据点中的每一个都作为一行添加到你的数据中 * 和 * 代码中!它'最好考虑一个更好的约定来组织数据,并使用代码绘制,该代码给出的结果与数据中的观察。
path geom在数据集中的点之间绘制线条,因此它对这样的数据很有用。
首先,您的数据框以适合Tidy Data的方式表示如下:

df <- data.frame(
  country = rep(c("Burundi", "Kenya"), each=6),
  threshold = c(
    0, 180,  180 , 242, 242, 10000,
    0, 72.2, 72.2, 144, 144, 10000
  ),
  rate = c(
    10, 10, 25, 25, 30, 30,
     3,  3, 20, 20, 30, 30
  )
)

整个绘图代码要简单得多,无论数据框df中有多少个观测值,都可以使用:

ggplot(data=df, aes(x=threshold, y=rate)) +
  geom_path(aes(color=country, linetype=country), size=0.6) +
  scale_linetype_manual(values = linetypes, breaks=c("Burundi", "Kenya")) +
  scale_color_manual(values = my_colors, breaks=c("Burundi", "Kenya"))

相关问题