我正在ggplot中绘制图,当我添加geom_line()层时,它包括2条线而不是一条。谁能帮助我了解它为什么这样做?

码:

library(ggplot2)
a <- data.frame(SubjectId=c(1:3, 1:3, 1:3, 1:3),
                Cycle=c(1,1.1,1.2, 2,2.1,2.2, 3,3.1,3.2, 4,4.1,4.2),
                Dose=c(sort(rep(1:3,3)), 3,3,3),
                DLT=c("No","No","Yes","No","No","No","No","Yes","Yes","No","Yes","Yes"))


ggplot(aes(x=Cycle, y=Dose, fill=DLT), data = a) +
  scale_fill_manual(values = c("white", "black")) +
  geom_line(colour="grey20", size=1) +
  geom_point(shape=21, size=5) +
  xlim(1, 4.5) +
  ylim(1, 4) +
  ylab("Dose Level") +
  theme_classic() +
  theme(axis.text   =element_text(size=10),
        axis.title  =element_text(size=12, face="bold", colour="grey20"),
        legend.text =element_text(size=10),
        legend.title=element_text(size=12, face="bold", colour="grey20"))


我只希望一条线按Cycle的顺序经过点,但是按Cycle排序根本不会改变这条线。我究竟做错了什么?

最佳答案

fill=DLT放在geom_point()部分中,而不是放在顶部。例如:

 ggplot(aes(x=Cycle, y=Dose), data = a) +
   scale_fill_manual(values = c("white", "darkred")) +
   geom_line(colour="grey20", size=1) +
   geom_point(shape=23, size=5, aes(fill=DLT)) +
   xlim(1, 4.5) +
   ylim(1, 4) +
   ylab("Dose Level") +
   theme_classic() +
   theme(axis.text   =element_text(size=10),
         axis.title  =element_text(size=12, face="bold", colour="grey20"),
         legend.text =element_text(size=10),
         legend.title=element_text(size=12, face="bold", colour="grey20"))

关于r - 当应该有两条线出现在我的图上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53404278/

10-09 17:08