R, ggplot: Change line type between 2 x axis factors

Viewed 319

I am trying to draw a plot with geom_line, and I want to change line type (solid to dashed) from a point to another point as in the picture.

my data frame look like this:

'data.frame':   33 obs. of  7 variables:
 $ pta.simnum      : int  2 2 3 3 5 5 8 8 15 15 ...
 $ dose            : num  100 100 200 200 400 400 700 700 1400 1400 ...
 $ group1          : chr  "arc" "nonarc" "arc" "nonarc" ...
 $ group2          : chr  "ei6" "ei6" "ei6" "ei6" ...
 $ pta.target      : num  0.25 0.25 0.5 0.5 1 1 2 2 4 4 ...
 $ pta.prop.success: num  0.925 0.935 0.925 0.935 0.925 0.935 0.901 0.904 0.901 0.904 ...
 $ toxic           : chr  "nontoxic" "nontoxic" "nontoxic" "nontoxic" ...

The idea is I want to illustrate non toxic dose and toxic dose in two types of line from low to high MIC of bacteria.

And this is my code:

graph.pta <- ggplot(data = pta.40, aes(x = as.factor(pta.target), y =  as.numeric(dose), group = group2, color = group2)) + 
  geom_point() + geom_line(data = filter(pta.40, toxic == "nontoxic"), size = 1,aes(linetype = "dashed")) + 
  geom_line(data = filter(pta.40, toxic == "toxic"), size = 1,aes(linetype = "solid")) +
  facet_wrap(~group1) 
graph.pta

and the result as shown in picture with these warning lines:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

I want to change line type from y value for each group according to the picture below however, the dashed line did not appear.

enter image description here

I suppose the result is not as expected since x axis is in factor, but the point I want to change to dashed line lies between two factors.

Is there any ways to plot this figure with my idea?

Thank you.

PS: I added part of my data as below:

structure(list(pta.simnum = c(2L, 2L, 3L, 3L, 5L, 5L, 8L, 8L, 
15L, 15L, 29L, 29L, 57L, 56L, 112L, 110L, 2L, 3L, 2L, 4L, 3L, 
6L, 5L, 10L, 8L, 19L, 14L, 36L, 26L, 71L, 50L, 141L, 98L), dose = c(100, 
100, 200, 200, 400, 400, 700, 700, 1400, 1400, 2800, 2800, 5600, 
5500, 11100, 10900, 100, 200, 100, 300, 200, 500, 400, 900, 700, 
1800, 1300, 3500, 2500, 7000, 4900, 14000, 9700), group1 = c("arc", 
"nonarc", "arc", "nonarc", "arc", "nonarc", "arc", "nonarc", 
"arc", "nonarc", "arc", "nonarc", "arc", "nonarc", "arc", "nonarc", 
"arc", "arc", "nonarc", "arc", "nonarc", "arc", "nonarc", "arc", 
"nonarc", "arc", "nonarc", "arc", "nonarc", "arc", "nonarc", 
"arc", "nonarc"), group2 = c("ei6", "ei6", "ei6", "ei6", "ei6", 
"ei6", "ei6", "ei6", "ei6", "ei6", "ei6", "ei6", "ei6", "ei6", 
"ei6", "ei6", "ei8", "ei8", "ei8", "ei8", "ei8", "ei8", "ei8", 
"ei8", "ei8", "ei8", "ei8", "ei8", "ei8", "ei8", "ei8", "ei8", 
"ei8"), pta.target = c(0.25, 0.25, 0.5, 0.5, 1, 1, 2, 2, 4, 4, 
8, 8, 16, 16, 32, 32, 0.125, 0.25, 0.25, 0.5, 0.5, 1, 1, 2, 2, 
4, 4, 8, 8, 16, 16, 32, 32), pta.prop.success = c(0.925, 0.935, 
0.925, 0.935, 0.925, 0.935, 0.901, 0.904, 0.901, 0.904, 0.901, 
0.904, 0.901, 0.902, 0.9, 0.9, 0.958, 0.958, 0.933, 0.92, 0.933, 
0.909, 0.933, 0.901, 0.926, 0.901, 0.915, 0.9, 0.91, 0.9, 0.908, 
0.9, 0.904), toxic = c("nontoxic", "nontoxic", "nontoxic", "nontoxic", 
"nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", 
"nontoxic", "nontoxic", "nontoxic", "nontoxic", "toxic", "toxic", 
"nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", 
"nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", "nontoxic", 
"nontoxic", "nontoxic", "nontoxic", "toxic", "toxic")), row.names = c(NA, 
33L), class = "data.frame")
1 Answers

Try this as an option, you have only 1 point in some cases so lines would not work. You can try with points based on conditions to color them:

library(ggplot2)
library(dplyr)
#Code
df <- pta.40 %>%
  mutate(Line=ifelse(dose>=6400 & group2=='ei6' & group1=='arc',1,
                     ifelse(dose>=7400 & group2=='ei6' & group1=='nonarc',2,
                            ifelse(dose>=7900 & group2=='ei8' & group1=='arc',3,
                                   ifelse(dose>=8200 & group2=='ei8' & group1=='nonarc',4,5))))) %>%
  mutate(pta.target=factor(pta.target,levels = sort(unique(pta.target)),
                           ordered = T))

#Plot
ggplot(data = df, aes(x = pta.target,
                      y =  as.numeric(dose),
                      group = group2,
                      color = interaction(factor(Line),group2),
                      linetype=factor(Line))) +
  geom_point() +
  facet_wrap(~group1)+
  labs(color='Var')

Output:

enter image description here

Related