I have a large dataframe with information about newspapers in certain regions ("cantons") in Switzerland. A minimum, reproducible example may look like this:
dataframe <- data.frame(canton=c("AG","AG","BE","LU","ZH","ZH"),
canton_id=c(19,19,2,3,1,1),
newspaper=c("AZ","ZOF","BZ","NLZ","AVU","LB"),
minimum=c("1999-12-03","2000-10-03","1998-12-03","1998-01-03","2011-04-03","2002-04-03"),
maximum=c("2009-09-29","2018-11-27","2018-11-27","2017-02-14","2018-11-27","2018-11-27"))
I need to plot a graphic with ggplot2 that should look similar to this one (please ignore the black points and consider this graphic beeing much bigger than the one resulting of the reproducible example):
Now, what have I already done? The closest I got was with the following code:
ggplot(dataframe) + geom_segment(aes(x=minimum, xend=maximum, y=newspaper, yend=newspaper, size = 1, color = canton))
Biggest Concern about my "solution": If a canton has more than one newspaper, I need them grouped by this canton, not in an alphabetical order. Do I need to work with factors?
I have already consulted the following questions:
How do you specifically order ggplot2 x axis instead of alphabetical order?
Order discrete x scale by frequency/value
If you need any further information, please let me know.
Maybe you can use a facet_grid()
approach to group by canton like this (I have used the data you shared):
library(ggplot2)
#Code
ggplot(dataframe) +
geom_segment(aes(x=minimum, xend=maximum, y=newspaper,
yend=newspaper, size = 1, color = canton),show.legend = F)+
facet_grid(canton~.,scales='free')+
theme_classic()+
theme(strip.background = element_blank())
Output: