Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

plotting multiple lines in the same plot using ggplot in R

I need to draw multiple line graphs that has some grouping. My data is follows:

    roc_mj_cmb_t1= c(0.8115,0.8060,0.8114,0.8340,0.8789,0.8743,0.8807,0.8838,
                 0.7946,0.7904,0.8245,0.8198,0.8721,0.8686,0.8876,0.8763,
                 0.8204,0.8146,0.7982,0.8350, 0.7977,0.8021,0.8137,0.8176



)

roc_mj_lab= c(rep("Y1_uni_cmb_test1",4),rep("Y1_Joint_test1",4),
          rep("Y1_uni_cmb_test2",4),rep("Y1_Joint_test2",4),
          rep("Y1_uni_both_test1",4), rep("Y1_uni_both_test2",4))


settin=c("setting 1","setting 2","setting 3","setting 4","setting 1","setting 2","setting 3","setting 4"
         ,"setting 1","setting 2","setting 3","setting 4","setting 1","setting 2","setting 3","setting 4"
         ,"setting 1","setting 2","setting 3","setting 4","setting 1","setting 2","setting 3","setting 4")


data_mj=data.frame(settin,roc_mj_cmb_t1,roc_mj_lab)

Then I obtained the line plot using this code:

    ggplot(data=data_mj,
       aes(y=roc_mj_cmb_t1,x=settin, group=roc_mj_lab))+
  geom_line(
         aes(color=roc_mj_lab,linetype=roc_mj_lab))
+scale_color_manual(values = c("blue", "red", "blue", "red","blue", "red")
       )+ylim(0.75,0.9) +scale_linetype_manual(values=c("longdash", "solid","longdash", "solid",
                                                        "dotted","dotted"))

Here is my output:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In this plot, I need to draw the lines starting from "Y1_XXX_test1" with same color and "Y1_XXX_test2" with same color. The 3 plots within "Y1_XXX_test1" need to be differentiated using line type. Similar thing needs to be apply for "Y1_XXX_test2" as well.

But based on this code, I have got different colors than I specified. (Ex: lines with yellow, green and pink colors) What could be the reason for that?

>Solution :

enter image description hereOne of your "+"’s is in the wrong location.
The + in this line: "+scale_color_manual.." needs to be at the end of the previous line.
When I cleaned up as below, I got your blue and red lines.

ggplot(data=data_mj,aes(y=roc_mj_cmb_t1,x=settin, group=roc_mj_lab))+
  geom_line(aes(color=roc_mj_lab,linetype=roc_mj_lab)) +
  scale_color_manual(values = c("blue", "red", "blue", "red","blue", "red"))+
  ylim(0.75,0.9) +
  scale_linetype_manual(values=c("longdash", "solid","longdash", "solid","dotted","dotted"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading