R语言 根据变量是负还是正区分geom_path中的线型

13z8s7eq  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(191)

在该数据中:

data <- structure(list(IPU_id = c(2L, 2L, 116L, 116L, 352L, 352L, 481L, 
481L, 583L, 583L, 1077L, 1077L, 1394L, 1394L, 1447L, 1447L, 1538L, 
1538L, 1982L, 1982L, 2459L, 2459L, 2729L, 2729L, 2763L, 2763L, 
2809L, 2809L, 3162L, 3162L, 3165L, 3165L, 3188L, 3188L, 3249L, 
3249L, 3454L, 3454L, 3618L, 3618L, 3780L, 3780L, 3796L, 3796L, 
3972L, 3972L, 4407L, 4407L, 4779L, 4779L, 5061L, 5061L, 6599L, 
6599L, 6614L, 6614L, 6977L, 6977L, 7135L, 7135L, 7462L, 7462L, 
8338L, 8338L, 8352L, 8352L, 8511L, 8511L, 8529L, 8529L, 9160L, 
9160L, 9524L, 9524L, 9547L, 9547L, 9966L, 9966L, 10147L, 10147L, 
10393L, 10393L, 10507L, 10507L, 10894L, 10894L, 10950L, 10950L, 
11550L, 11550L, 11563L, 11563L, 11571L, 11571L, 11718L, 11718L, 
11741L, 11741L, 11912L, 11912L, 12062L, 12062L, 12112L, 12112L, 
12116L, 12116L, 12129L, 12129L, 12161L, 12161L, 12718L, 12718L, 
12767L, 12767L, 12800L, 12800L, 12809L, 12809L, 13580L, 13580L, 
13829L, 13829L, 13979L, 13979L, 14002L, 14002L, 14609L, 14609L, 
18862L, 18862L, 19116L, 19116L, 19804L, 19804L, 20654L, 20654L, 
24181L, 24181L, 26543L, 26543L, 26562L, 26562L, 27002L, 27002L, 
27050L, 27050L, 27068L, 27068L, 27199L, 27199L, 27354L, 27354L, 
27632L, 27632L, 27677L, 27677L, 27697L, 27697L, 28288L, 28288L
), comparison = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L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1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("Answerer", 
"NotAnswerer"), class = "factor"), value = c(-0.0431558441558438, 
1.70782692307692, 0.684046511627906, 0.423953124999999, 0.302268456375839, 
-0.309683333333333, 0.792993421052627, -0.871427152317877, 2.55586178861788, 
-0.85030081300813, -0.241509900990097, 0.026694999999999, 0.880453020134229, 
-0.298657718120805, -0.0876920529801324, -0.250344370860927, 
-6.02329885057471, -8.22881976744186, -6.48995, 1.035525, 9.35135416666668, 
-2.60511643835616, 0.643900662251654, 0.167112582781379, 0.572819078947369, 
-2.0765165562914, 0.462728395061728, -0.141359756097561, 0.76575, 
-1.60808333333333, -0.894208609271523, -0.786271523178809, 0.754248201438848, 
-0.160610714285716, -0.338933884297521, -1.21221008403362, 6.022116, 
-0.524599999999991, 0.425596774193548, 0.163237012987012, 0.189037634408601, 
-2.02574175824177, 0.1978, 1.16077962962963, -1.45908116883117, 
0.964378289473683, 0.421305555555556, 0.540716783216783, 0.16981683168317, 
-0.798830000000001, -5.24771590909091, 0.00527067669172944, -0.0771428571428602, 
1.30525, -1.54231666666667, -0.87164, -1.6384238410596, -0.363324503311258, 
0.46028145695364, -0.10932119205298, 0.925983050847458, 2.02564406779661, 
0.527496212121212, -0.55037786259542, 0.075385869565218, 0.672869565217391, 
-3.55737931034484, 0.261267857142857, 0.362056291390728, 0.239115894039735, 
0.116307947019869, 0.216654605263158, 0.732013422818791, 0.709734899328861, 
0.362520134228187, 0.092070469798658, 1.32523305084746, 0.207796610169491, 
-0.440720930232559, -1.91472265625, 0.596914473684205, 0.68512582781457, 
3.48474796747966, -0.790289156626509, 0.858354651162783, 4.8597356321839, 
4.24323509933774, -1.68983774834437, 1.95657432432433, -1.80357432432432, 
0.134786821705425, -0.463600775193796, -0.222075342465754, -0.137592465753425, 
7.38660927152317, -4.3473, 0.935841059602654, -8.79423026315788, 
-0.123965034965031, 0.139277777777776, 0.435614678899091, -0.0393564814814806, 
2.04550442477876, 0.544691304347827, 0.398654929577465, -1.11304929577466, 
0.509702702702702, -5.98748648648649, 1.33939403973509, -11.2562350993377, 
0.402916666666664, -2.24811394557824, 6.45144, -1.86279591836734, 
-2.86064569536425, -0.778245033112582, 2.69490604026845, 0.175989999999999, 
-0.0124668874172132, 0.594857615894042, 2.14110317460318, 4.49728225806452, 
-8.18573913043478, -0.88930530973451, -0.753973856209148, 0.496088235294117, 
-0.402965346534655, 0.181366336633662, -0.451240000000001, 1.91439, 
-0.633221476510065, 0.292332214765101, -8.19086139455783, -0.494641176470588, 
0.908275000000001, -10.3464235714286, -0.206032894736843, -7.97076158940397, 
1.47209302325581, -1.92366091954023, -0.704506622516558, -0.226966887417218, 
2.37554, 2.51840333333333, 1.99034437086093, 11.2908609271523, 
1.10772023809525, -3.53189880952381, 0.220096026490063, -0.898711920529798, 
0.700872093023255, -11.4639803921569, -3.12411920529801, 8.04174685430463, 
-6.07138333333333, 1.2730100671141, -1.07498230088496, 0.277022123893805, 
-3.62009154929577, 2.01665277777778), name_num = c(1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 
1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675)), row.names = c(NA, 
-160L), class = c("tbl_df", "tbl", "data.frame"))

这个图:

library(tidyverse)
library(tidyquant)
library(ggdist)
library(ggthemes)

box_width <- .12
jitter_width <- .025

pj <- position_jitter(seed = 1, width = jitter_width, height = 0)

data %>%
    
  ggplot(aes(x = name, y = value, fill = name)) +
  
   # half-violin:
   stat_halfeye(
     aes(
       justification = ifelse(name == "Answerer", 1.2, -.2),
       side = ifelse(name == "Answerer", "left", "right")
     ),
     adjust = 0.5,
     .width = 0,
     point_colour = NA
   ) +
  
  # boxplot:
  geom_boxplot(
    width = box_width,
    alpha = 0.5,
  ) +
  scale_fill_manual(values = c(
    "Answerer" = "slateblue",
    "NotAnswerer" = "lawngreen"
  )) +
  guides(fill = "none", color = "none")+
  
  # data points:
  geom_point(
    aes(x = name_num, color = factor(comparison)),
    alpha = 0.4, col = "grey40",
    position = pj
  ) +

  # connecting lines:
  geom_path(aes(x = name_num, group = IPU_id, color = factor(comparison)),
            alpha = 0.4,
            col = "grey40",
            position = pj,
  )

我想根据变量comparison是负(0)还是正(1)来区分geom_path中的线型:例如,如何得到折线,其中comparison is positive and a **dotted line** where the比较`为负?

k5ifujac

k5ifujac1#

您必须将数据集中的某些内容Map到geom_path()调用中的linetype美学。在共享的数据集中,没有一个很好的方法来做到这一点,所以这里有一个方法,通过这些步骤:

  • 使用此斜率信息汇总您的数据(“NotAnswerer”-“Answerer”的结果),
  • 将新的汇总数据合并回原始数据集
  • 重写geom_path()部分以Map到linetype,以及
  • 手动设置linetype以匹配新值

下面是完整的代码,并有一些注解可以帮助您:

# calculating slopes
df <- data %>% select(IPU_id, comparison, name, value) %>%
  pivot_wider(names_from = "name", values_from = "value") %>%
  mutate(slope_num = NotAnswerer - Answerer)

# summarizing and gathering together the slope per each IPU_id
df <- df %>%
  group_by(IPU_id) %>%
  summarize(slope_name = ifelse(sign(slope_num) < 0, "negative", "positive"))

# merging dataset back - keeping all values in original dataset
merged <- left_join(data, df, by="IPU_id")

这是情节的新代码。为了简洁起见,我在这里只表示geom_path()部分。

geom_path(
    aes(
      x = name_num, group = IPU_id,
      color = factor(comparison),
      linetype = slope_name),  # here's our new mapping
    show.legend = FALSE,   # note, removing legend here
    alpha = 0.4, col = "grey40",
    position = pj
  ) +

  # manually setting values to map to slope
  scale_linetype_manual(values=c("negative"="dashed", "positive"="solid"))

相关问题