简体   繁体   中英

How do I plot multiple lines of different types on the same chart with ggplot & R

I am trying to plot two lines on the same chart using ggplot and R. I want one line to be a solid black line (not points) and the other line to actually be a set of red data points ("dotted"). I find lots of information about how to plot the same type of line in different colors but I cannot seem to find how one specifies two different types of lines of different colors on the same chart. A toy set of data might be, eg:

my_data <- read.table(header=TRUE, text='
supp dose length
  OJ  0.5  13.23
  OJ  1.0  22.70
  OJ  2.0  26.06
  VC  0.5   7.98
  VC  1.0  16.77
  VC  2.0  26.14
')

Suppose I want dose on the x axis and I want OJ on the y axis to be one curve which is solid black (no dots) and I want VC on the y axis to be represented on the chart as 3 dots at 7.98, 16.77, and 26.14. What ggplot code will do that?

If you want different geom for different conditions, one solution is to subset your dataframe for each geom :

library(ggplot2)

ggplot(my_data, aes(x = dose, y = length))+
  geom_point(data = subset(my_data, supp == "VC"))+
  geom_line(data = subset(my_data, supp == "OJ"))

在此处输入图片说明

Does it answer your question ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM