R语言 几何线():样式图例

5uzkadbs  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(238)

我正在尝试使用以下代码绘制图:

p <- ggplot()+
  geom_line(fit.pre.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (before SOEs)", linetype = "Predicted (before SOEs)"), size = 1) +
  geom_line(fit.mid.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (during SOEs)", linetype = "Predicted (during SOEs)"), size = 1) +
  geom_line(fit.post.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (after SOEs)", linetype = "Predicted (after SOEs)"), size = 1) +
  geom_line(fit.pre.count, mapping = aes(x = x, y = y, group = group, col = "Counterfactual (before SOEs)", linetype = "Counterfactual (before SOEs)"), size = 1) +
  geom_line(fit.mid.count, mapping = aes(x = x, y = y, group = group, col = "Counterfactual (during SOEs)", linetype = "Counterfactual (during SOEs)"), size = 1) +
  scale_linetype_manual(values = c("Predicted (before SOEs)" = "solid", "Predicted (during SOEs)" = "solid", "Predicted (after SOEs)" = "solid",
                                   "Counterfactual (before SOEs)" = "dashed", "Counterfactual (during SOEs)" = "dashed")) +
  scale_color_manual(values = c("Predicted (before SOEs)" = "steelblue4", "Counterfactual (before SOEs)" = "steelblue4",
                                "Predicted (during SOEs)" = "red", "Counterfactual (during SOEs)" = "red", "Predicted (after SOEs)" = "seagreen4"))

图例是这样的:

1.为什么单独显示线型和颜色,以及如何合并它们?
1.我看到另一个例子,“dashed”实际上看起来像“dashed”,而不仅仅是比“solid”图例短一点。为什么以及如何修复这个问题?
1.现在,图例出现在图的右边,有没有办法把它缩小,放在左下角?
我希望指定颜色和线型,如下所示:
“预测(SOE前)":钢蓝4,固体
“预测(SOE期间)":红色,固体
“预测(SOE后)":海绿色4,固体
“反事实(在SOE之前)":钢蓝色4,虚线
“反事实(SOE期间)":红色,虚线
有人能告诉我如何解决这个问题吗?
正如评论中所建议的那样,我尝试了这个:

p <- ggplot
  geom_line(fit, mapping = aes(x = x, y = y, group = group, col = group, linetype = group), size = 1) +
  scale_linetype_manual(values = c("Predicted (before SOEs)" = "solid", "Predicted (during SOEs)" = "solid", "Predicted (after SOEs)" = "solid",
                                   "Counterfactual (before SOEs)" = "dashed", "Counterfactual (during SOEs)" = "dashed")) +
  scale_color_manual(values = c("Predicted (before SOEs)" = "steelblue4", "Counterfactual (before SOEs)" = "steelblue4",
                                "Predicted (during SOEs)" = "red", "Counterfactual (during SOEs)" = "red", "Predicted (after SOEs)" = "seagreen4"))

数据现在如下所示:

# A tibble: 10 × 3
   x           y group                       
   <chr>   <dbl> <chr>                       
 1 2018-01  24.7 Predicted (before SOEs)     
 2 2020-03  25.1 Predicted (before SOEs)     
 3 2020-04  25.3 Predicted (during SOEs)     
 4 2021-09  25.0 Predicted (during SOEs)     
 5 2021-10  25.2 Predicted (after SOEs)      
 6 2022-07  25.2 Predicted (after SOEs)      
 7 2020-04  25.1 Counterfactual (before SOEs)
 8 2021-09  25.4 Counterfactual (before SOEs)
 9 2021-10  25.0 Counterfactual (during SOEs)
10 2022-07  24.9 Counterfactual (during SOEs)

然后我发现可以通过在theme()中添加以下代码来更改图例的位置和大小:

legend.text=element_text(size=10),
legend.justification=c(0.05,0.05),legend.position=c(0.05,0.05),legend.title=element_blank()

现在,1和3都解决了,但是我的虚线图例看起来仍然很奇怪......这里可能有什么问题?

ivqmmu1c

ivqmmu1c1#

您的线型在图例索引中无法正确显示的原因是图例索引中的线条太大,无法看到linetype的美观效果。此问题的解决方案是显式定义要在图例中使用的较小的size。默认情况下,图例索引美观效果与geom_*()命令绘制的效果匹配。但是您可以重新定义图例中使用的美学效果,或者更改键的大小来解决这个问题。

问题示例

下面的例子很好地说明了这个问题和上面的修复方法。首先,下面是基本的图表:
文库(ggplot2)

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species, linetype=Species))
p + geom_line()

在上图中,您可以轻松区分图例中的linetypes。请注意,图例键的大小和美观程度与主面板中显示的内容相匹配。让我们看看,当我们为geom_line()中的线条定义更大的大小时会发生什么:

p + geom_line(size=1.8)

看起来熟悉吗?相对于图例键大小而言,较大的线条大小会导致图例键看起来不正确。您可以通过 * 或者 * 增大图例键或重新定义用于绘制图例的外观来解决此问题。

修复1:更改图例大小

要更改图例的大小,可以使用theme()元素legend.key.sizelegend.key.widthlegend.key.height。我喜欢这里的legend.key.width,因为我们正在查看水平线:

p + geom_line(size=1.8) +
  theme(legend.key.width = unit(40, 'pt'))

修复2:更改图例外观

一个可能更好的解决方案(在我看来)是明确定义图例中使用的美学,这可以用来告诉ggplot在图例中"把它画得更小":

p + geom_line(size=1.8) +
  guides(
    linetype=guide_legend(override.aes = list(size=1))
  )

要使用的修复取决于特定的打印、与图例相比的外观以及个人偏好。

相关问题