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
  • 23,419
  • 5
  • 52
  • 85
srg
  • 67
  • 6
  • In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Mar 09 '21 at 19:56
  • Facets are separate graphs. Seems like you don't want facets... – Gregor Thomas Mar 09 '21 at 20:00
  • if I don't use facets, how can I represent/label Months data April vs May in my graph ? – srg Mar 09 '21 at 20:03
  • It appears like R ggplot does not offer a solution to draw continuos line graph and differentiate/label/group data on x-axis ? – srg Mar 09 '21 at 20:39

1 Answers1

0

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
  • 11,680
  • 1
  • 10
  • 21
  • 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