R语言 几何点和几何线如何搭配position_dodge?[duplicate]

niwlg2el  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(160)

此问题在此处已有答案

Is it possible to jitter two ggplot geoms in the same way?(4个答案)
How to combine position_dodge() and geom_line() with overlapping groupings?(2个答案)
using position_dodge with geom_line(1个答案)
昨天关门了。
此帖子已于昨天编辑并提交审核,未能重新打开帖子:
原始关闭原因未解决
使用position_dodge时,几何点(点)和几何线(线)在ggplot 2中的位置不成对

下面是我的代码:

ggplot(data = datae.19.26, aes(x=as.factor(assay.tem), y=change.fitness ))+
  geom_point(aes(group= as.factor(evolution.tem)), position = position_dodge(width = 0.3))+
  xlab(expression(paste("Assasy environment"))) + 
  ylab(expression(paste("Change in fitness"))) + 
  labs(shape=expression("Evolution\nenvironment", degree~"C"))+
  geom_line(aes(linetype=as.factor(evolution.tem),group= tag),position = position_dodge(width = 0.3))+
  theme_classic()
)

这是我的数据

标签=块+演化.tem +组
我的数据(用于测试):

assay.tem <- c(19,19,19,19,19,19,19,19,26,26,26,26,26,26,26,26)
evolution.tem <- c(19,19,26,26,19,19,26,26,19,19,26,26,19,19,26,26)
tag <- c("A19a","A19b","A26a","A26b","B19a","B19b","B26a","B26b","A19a","A19b","A26a","A26b","B19a","B19b","B26a","B26b")
change.fitness <- c(0.148000525,0.422677262,0.360426885,0.336559874,0.611094594,0.515070536,0.173537012,0.325389861,0.441893408,0.689221781,0.573697751,0.564322921,0.709060167,0.911127005,0.613946604,0.572489802)

datae.test <- data.frame (assay.tem,evolution.tem,tag,change.fitness)

我猜默认的点的位置躲避是'as.factor(evolution.tem)',而线的位置躲避是'tag',但是我该怎么解决这个问题呢?
(note线和点的组不相同!与using position_dodge with geom_line不同)

lfapxunr

lfapxunr1#

试试这个:
我们不需要position_dodge
基本上:这就是答案:

df %>% 
  ggplot(aes(x = factor(assay.tem), y=change.fitness, group=tag))+
  geom_point()+
  geom_line()

可以扩展的基本方法:由于你不提供所有的数据,我过滤了相关对:

df %>% 
  filter(row_number() %in% c(1,2,13, 14)) %>% 
  ggplot(aes(x = factor(assay.tem), y=change.fitness))+
  geom_point()+
  geom_line(aes(group=tag))+
  xlab(expression(paste("Assasy environment"))) + 
  ylab(expression(paste("Change in fitness"))) + 
  labs(shape=expression("Evolution\nenvironment", degree~"C"))+
  theme_classic()

数据:

df <- structure(list(species = c("E", "E", "E", "E", "E", "E", "E", 
"E", "E", "E", "E", "E", "E", "E"), assay.tem = c(19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 26L, 26L), block = c("A", 
"A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "A", "A"
), evolution.tem = c(19L, 19L, 26L, 26L, 19L, 19L, 26L, 26L, 
19L, 19L, 26L, 26L, 19L, 19L), group = c("a", "b", "a", "b", 
"a", "b", "a", "b", "a", "b", "a", "b", "a", "b"), tag = c("A19a", 
"A19b", "A26a", "A26b", "B19a", "B19b", "26a", "B26b", "C19a", 
"c19b", "C26a", "c26b", "A19a", "A19b"), change.fitness = c(0.1480005, 
0.4226773, 0.3604269, 0.3365599, 0.6110946, 0.5150705, 0.173537, 
0.3253899, 0.4142653, 0.3063598, 0.221773, 0.186416, 0.4418934, 
0.6892218)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "7", "8", "9", "10", "13", "14", "15", "16", "19", "20"))

相关问题