1

My data :

Plate   Month   Day     Concentration
A       April   1       17.1094
B       April   2       16.001
C       April   3       17.9501
D       April   4       18.3686
E       April   5       18.3133
F       April   6       19.1189
G       May     1       16.0423
H       May     2       16.3614
I       May     3       18.5723
J       May     4       19.1091
K       May     5       17.6863
L       May     6       18.2647

Using geom_line I was able to draw a line graph connecting all dots in a nice plot as follows :

myData <- data.frame(read.table("myData.txt", sep="\t", header=TRUE))

ggplot(myData, aes(x = Plate, y = Concentration, group=1)) + geom_point() + geom_line()

enter image description here

I differentiated data based on Month using facet panels, and then draw a line graph as follows. Here, the line plot is disconnected after the last day in April month. How can I draw line plot that draws across facets i.e. from April to May ?

ggplot(myData, aes(x = Day, y = Concentration)) + facet_wrap(~ Month, nrow = 1) + geom_line() + geom_point() 

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
srg
  • 67
  • 7

1 Answers1

1

You could fake it:

mydata <- "Plate   Month   Day     Concentration
A       April   1       17.1094
B       April   2       16.001
C       April   3       17.9501
D       April   4       18.3686
E       April   5       18.3133
F       April   6       19.1189
G       May     1       16.0423
H       May     2       16.3614
I       May     3       18.5723
J       May     4       19.1091
K       May     5       17.6863
L       May     6       18.2647"

myData <- data.frame(read_table(mydata))

myData <- myData %>% mutate(plot_day = seq_along(Day))
rect_data <- myData %>% group_by(Month) %>% 
  summarise(data.frame(xmin = min(plot_day), xmax=max(plot_day))) %>% 
  mutate(ymin = 19.25, ymax=19.5)

ggplot() + 
  geom_point(data = myData, aes(x = plot_day, y = Concentration)) + 
  geom_rect(data=rect_data, aes(ymin=ymin, ymax=ymax, xmin=xmin-c(.55, .45), xmax=xmax+c(.45, .55)), 
            fill="gray75", col="transparent") + 
  geom_text(data = rect_data, aes(x=c(3.5, 9.5), y=19.375, label = c("April", "May"))) + 
  coord_cartesian(xlim=c(.449, 12.551), ylim=c(15.75, 19.501), expand=0) + 
  scale_x_continuous(breaks = seq(2,12, by=2), 
                     labels=c("2", "4", "6", "2", "4", "6")) + 
  geom_vline(xintercept=6.5, col="white", size=2) +
  geom_line(data = myData, aes(x = plot_day, y = Concentration)) + 
  labs(x="Day")   

enter image description here

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • Thank you DaveArmstrong for this temporary solution to make the graph appear like having facets without using any grids. This works for customizing on few plots, but it becomes cumbersome to apply this customization for multiple plots recursively. – srg Mar 29 '21 at 19:21