Отсутствующие точки и стиль линии в легенде рисунка с ggplot2 - PullRequest
1 голос
/ 25 июля 2020

После выполнения следующих команд:

Population <- c("A", "A", "A", "A", "B", "B", "B", "B")
Group <- rep(c("Experimental", "Experimental", "Control", "Control"), 2)
wave <- rep(c("Pretest", "Posttest"), 4)
outcome <- c(-.3, -.2, -.3, .4, -.6, -.5, -.6, .6)
ci <- rep(c(.13, .14), 4)
df <- data.frame(Population, Group, wave, outcome, ci)
df<span class="math-container">$wave <- factor(df$</span>wave,levels = c('Pretest','Posttest'))

library(ggplot2)
pd <- position_dodge(0.1)
ggplot(df, aes(x = wave, y = outcome, color = interaction(Population, Group), shape = Group, group = interaction(Population, Group))) +
  geom_errorbar(aes(ymin = outcome - ci, ymax = outcome + ci), width = .25, position = pd, size=.5) +
  geom_line(aes(linetype = Group), position = pd, size=1, show.legend = FALSE) +
  geom_point(position = pd, size = 3.5, fill = "white", stroke = 1.25, show.legend = FALSE) +
  scale_color_manual(values = c("#000000", "#606060", "#000000", "#606060")) +
  scale_shape_manual(values = c(23, 21)) +
  coord_cartesian(xlim = c(1.4, 1.6), ylim = c(-.91, .91)) +
  labs(title = "Outcomes by Population and Study Group", x = "Time", y = "Outcome\nLower scores denote fewer instances", color = "Population and Study Group")  +
  theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(color = "black"), axis.text.y = element_text(color = "black"), panel.background = element_rect(fill = "#F0F0F0"))

Я генерирую фигуру, не имеющую символов точек или правильных стилей линий в легенде:

введите описание изображения здесь

Как я могу:

  • добавить точки, показанные на самом рисунке, в легенду и
  • сделать так, чтобы линии легенды отражали то, что некоторые из пунктирных линий на рисунке?

TYIA.

1 Ответ

1 голос
/ 05 августа 2020

Самый простой способ - создать другую переменную, которая отражала бы взаимодействие, а не создавать ее на лету. Если мы построим график шаг за шагом, ниже будут отображаться точки и полосы ошибок:

library(ggplot2)
pd <- position_dodge(0.1)
df<span class="math-container">$grp = paste(df$</span>Population,df$Group,sep=".")

g = ggplot(df, aes(x = wave, y = outcome, color = grp, shape = grp))+
geom_errorbar(aes(ymin = outcome - ci, ymax = outcome + ci), width = .25, position = pd, size=.5) +
geom_point(position = pd, size = 3.5, fill = "white", stroke = 1.25) +
scale_color_manual(values = c("#000000", "#000000","#606060", "#606060")) +
scale_shape_manual(values = c(23,21,23,21)) +
coord_cartesian(xlim = c(1.4, 1.6), ylim = c(-.91, .91)) +
labs(title = "Outcomes by Population and Study Group", x = "Time", y = "Outcome\nLower scores denote fewer instances") +
theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(color = "black"), 
axis.text.y = element_text(color = "black"), panel.background = element_rect(fill = "#F0F0F0"))

print(g)

enter image description here

Then add the line while specifying the legend:

g + 
geom_line(inherit.aes=FALSE,aes(x = wave, y = outcome,group=grp,linetype=grp)) +
scale_linetype_manual(values=c("solid","dashed","solid","dashed"))

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...