1
votes

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, e.g.:

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?

1

1 Answers

1
votes

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"))

enter image description here

Does it answer your question ?