R语言 如何更改ggplot的形状?

rdlzhqv9  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(293)

对于Difference in Difference方法,我得出了以下数据:

df <- structure(list(Class = structure(c(1L, 1L, 2L, 2L), levels = c("PovCon", 
"PovDeCon"), class = "factor"), After_2015 = structure(c(1L, 
2L, 1L, 2L), levels = c("Before 2015", "After 2015"), class = "factor"), 
    mean_VLP = c(16.5314094033954, 25.3785125225305, 22.4646340695607, 
    19.5147929056452), se_duration = c(3.72103200892531, 8.17273164333138, 
    4.03966402631034, 2.56248212580638), upper = c(23.824632140889, 
    41.39706654346, 30.382375561129, 24.5372578722257), lower = c(9.23818666590181, 
    9.35995850160102, 14.5468925779924, 14.4923279390647)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), groups = structure(list(
    Class = structure(1:2, levels = c("PovCon", "PovDeCon"), class = "factor"), 
    .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

对于图形表示,我使用了以下代码:

ggplot(df, aes(x = After_2015, 
                          y = mean_VLP, 
                         color = Class)) +
   geom_pointrange(aes(ymin = lower, ymax = upper), size = 1) + 
   geom_line(aes(group = Class))

现在,按照杂志的要求,我需要所有的东西都是白色的,没有颜色!
因此,理想情况下,我希望得到两个Class的两种不同形状和连接两个相应数据点的不同线型。
我使用了以下代码来更改这些行:

ggplot(plot_data_VLP, aes(x = After_2015, 
                          y = mean_VLP, 
                          shape = Class,
                          linetype = Class)) +
  geom_pointrange(aes(ymin = lower, ymax = upper), size = 1) + 
  geom_line(aes(group = Class)))

如何使用向上和向下的三角形更改形状?
请帮忙,谢谢你的时间。

mfuanj7w

mfuanj7w1#

要获得不同的形状,您必须Map到shape美学:

library(ggplot2)

base <- ggplot(df, aes(
  x = After_2015,
  y = mean_VLP,
  linetype = Class
)) +
  geom_pointrange(aes(ymin = lower, ymax = upper, shape = Class), size = 1) +
  geom_line(aes(group = Class))
base

UPDATE:要获得向上和向下的三角形,您可以使用手动缩放,对于形状,我使用一些UTF8(https://www.compart.com/en/unicode/block/U+25A0):

base + scale_shape_manual(values = c("\u25B2", "\u25BC"))

相关问题