【问题标题】:How to change the shapes for ggplot?如何更改ggplot的形状?
【发布时间】:2022-11-29 15:24:10
【问题描述】:

对于差分法,我得出了以下数据:

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)))

如何更改带有向上和向下三角形的形状?

请帮忙,谢谢你的时间。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    要获得不同的形状,您必须在 shape 美学上进行映射:

    library(ggplot2)
    
    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多