another variation on facet_wrap ordering - r

I've got something like 20 facets on a geom_line ggplot2 plot, with an overlaid geom_rect based on timeseries data, all with a facet_wrap. I constantly need to update my plots, and the order of my facets ultimately may need to change on a daily basis.
My question is: is it possible to order my facets using my time-series data in geom_rect? I.e. make the first facet the one that has the first geom_rect shaded area, and so on and so forth?
Here is my code: x-axis is date, y-axis is incidence3, and faceted by geo....
ggplot () +
geom_rect(data=total,
aes(xmin=as.Date(xmin),
xmax=as.Date(xmax),
ymin=-Inf,
ymax=Inf),
fill='light blue',
alpha=0.3) +
ylab("incidence") + xlab("time") +
facet_wrap(~geo) +
geom_line(data=total, aes(x=as.Date(date), y=incidence3)) +
facet_wrap(~geo, ncol=2, scale = "free_y")

Related

Is there a way of overlaying two ggplot set of panels without rescaling any of the y-axes?

I want to overlay two sets of ggplot panels (each panel is a different country) into one, single ggplot panel, without any rescaling of any of the two plots, but ggplot rescales either one or the other.
I have tried using only one ggplot to include both variables, by doing ggplot(df, aes(x=t, y=a)), and, within that ggplot, then using geom_point and geom_smooth for the second variable (y=b), but this rescales variable a.
# plot 1
g <-ggplot(df, aes(x=year, y=a))
p <-g + geom_point(alpha=0.7) + geom_smooth(method="auto") + facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('a')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
# plot 2
a <-ggplot(df, aes(x=year, y=b))
b <-a + geom_point(alpha=0.7, color="green") + geom_smooth(method="auto", color="darkgreen") +
facet_wrap(~country, scales="free") + theme_bw() +
xlab("Year") + ylab(bquote('b')) +
scale_x_continuous(breaks=seq(1960, 2020, 15))
I expect to be able to overlay these two ggplots into a single set of panels, with both y-axes appearing exactly as they appear when they're plotted alone (including units). I would then need to somehow make one of the y-axis appear to the right of the panels, so I have two y-axes, one at each side.
Image 1. ggplot rescales left y-axis. I don't want this to happen.
Image 2. What I want instead is to be able to somehow merge each of these images to get a single panel per country, displaying both the green and the blue lines with the scales that appear here.

How to get boxplots using ggplot to overlap instead of faceting

I am new to ggplot, and using ggplot to show box plots of my data corresponding to different types like this. There are four types. I found that I can use facet_wrap to generate four different graphs.
ggplot(o.xp.sample, aes(power, reduction, fill=interaction(type,power), dodge=type)) +
stat_boxplot(geom ='errorbar')+
geom_boxplot() +
facet_wrap(~type)
My question is, I want to combine all the four graphs into one graph such that each type has a different color (and slightly transparent to show other plots through). Is this possible?
Here is the data https://gist.github.com/anonymous/9589729
Try this:
library(ggplot2)
o.xp.sample = read.csv("C:\\...\\data.csv",sep=",")
ggplot(o.xp.sample, aes(factor(power), reduction, fill=interaction(type,power), dodge=type)) +
stat_boxplot(geom ='errorbar') +
geom_boxplot() +
theme_bw() +
guides(fill = guide_legend(ncol = 3)) #added line as suggested by Paulo Cardoso

Scatter plot and boxplot overlay

Based on the previous post ggplot boxplots with scatterplot overlay (same variables),
I would like to have one boxplot for each day of week instead of two boxplots while have scatter points on it with different colour.
The code will be like:
#Box-plot for day of week effect
plot1<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend)) + geom_boxplot()
#Scatter with specific colors for day of week
plot2<-ggplot(dodgers, aes(x=ordered_month, y=Attend, colour=Bobblehead, size=1.5)) + geom_point()
#Box-ploy with Scatter plot overlay
plot3<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend, colour=Bobblehead)) + geom_boxplot() + geom_point()
And the result would be:
1, scatter plot
2, boxplot plot
3, combined plot
Put color= inside the aes() of geom_point() and remove it from ggplot() aes(). If you put color= inside the ggplot() then it affects all geoms. Also you could consider to use position dodge to separate points.
Example with mtcars data as OP didn't provide data.
ggplot(mtcars,aes(factor(cyl),mpg))+geom_boxplot()+
geom_point(aes(color=factor(am)),position=position_dodge(width=0.5))

Plotting continuous and discrete series in ggplot with facet

I have data that plots over time with four different variables. I would like to combine them in one plot using facet_grid, where each variable gets its own sub-plot. The following code resembles my data and the way I'm presenting it:
require(ggplot2)
require(reshape2)
subm <- melt(economics, id='date', c('psavert','uempmed','unemploy'))
mcsm <- melt(data.frame(date=economics$date, q=quarters(economics$date)), id='date')
mcsm$value <- factor(mcsm$value)
ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line() +
facet_grid(variable~., scale='free_y') +
geom_step(data=mcsm, aes(date, value)) +
scale_y_discrete(breaks=levels(mcsm$value))
If I leave out scale_y_discrete, R complains that I'm trying to combine discrete value with continuous scale. If I include scale_y_discreate my continuous series miss their scale.
Is there any neat way of solving this issue ie. getting all scales correct ? I also see that the legend is alphabetically sorted, can I change that so the legend is ordered in the same order as the sub-plots ?
Problem with your data is that that for data frame subm value is numeric (continuous) but for the mcsm value is factor (discrete). You can't use the same scale for numeric and continuous values and you get y values only for the last facet (discrete). Also it is not possible to use two scale_y...() functions in one plot.
My approach would be to make mcsm value as numeric (saved as value2) and then use them - it will plot quarters as 1,2,3 and 4. To solve the problem with legend, use scale_color_discrete() and provide breaks= in order you need.
mcsm$value2<-as.numeric(mcsm$value)
ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, value2)) +
scale_color_discrete(breaks=c('psavert','uempmed','unemploy','q'))
UPDATE - solution using grobs
Another approach is to use grobs and library gridExtra to plot your data as separate plots.
First, save plot with all legends and data (code as above) as object p. Then with functions ggplot_build() and ggplot_gtable() save plot as grob object gp. Extract from gp only part that plots legend (saved as object gp.leg) - in this case is list element number 17.
library(gridExtra)
p<-ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, value2)) +
scale_color_discrete(breaks=c('psavert','uempmed','unemploy','q'))
gp<-ggplot_gtable(ggplot_build(p))
gp.leg<-gp$grobs[[17]]
Make two new plot p1 and p2 - first plots data of subm and second only data of mcsm. Use scale_color_manual() to set colors the same as used for plot p. For the first plot remove x axis title, texts and ticks and with plot.margin= set lower margin to negative number. For the second plot change upper margin to negative number. faced_grid() should be used for both plots to get faceted look.
p1 <- ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y')+
theme(plot.margin = unit(c(0.5,0.5,-0.25,0.5), "lines"),
axis.text.x=element_blank(),
axis.title.x=element_blank(),
axis.ticks.x=element_blank())+
scale_color_manual(values=c("#F8766D","#00BFC4","#C77CFF"),guide="none")
p2 <- ggplot(data=mcsm, aes(date, value,group=1,col=variable)) + geom_step() +
facet_grid(variable~., scale='free_y')+
theme(plot.margin = unit(c(-0.25,0.5,0.5,0.5), "lines"))+ylab("")+
scale_color_manual(values="#7CAE00",guide="none")
Save both plots p1 and p2 as grob objects and then set for both plots the same widths.
gp1 <- ggplot_gtable(ggplot_build(p1))
gp2 <- ggplot_gtable(ggplot_build(p2))
maxWidth = grid::unit.pmax(gp1$widths[2:3],gp2$widths[2:3])
gp1$widths[2:3] <- as.list(maxWidth)
gp2$widths[2:3] <- as.list(maxWidth)
With functions grid.arrange() and arrangeGrob() arrange both plots and legend in one plot.
grid.arrange(arrangeGrob(arrangeGrob(gp1,gp2,heights=c(3/4,1/4),ncol=1),
gp.leg,widths=c(7/8,1/8),ncol=2))

how to remove line from fill scale legend using geom_vline and geom_histogram r ggplot2

Basics:
Using R statistical software, ggplot2, geom_vline, and geom_histogram to visualize some data. The issue is with the legend keys.
I'm trying to plot a pair of histograms from some stochastic simulations, and on top of that plot a couple of lines representing the result of a deterministic simulation. I've got the data plotted, but the legend keys for the histograms have an unnecessary black line through the middle of them. Can you help me remove those black lines? Some sample code reproducing the issue is here:
df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ),
rating = c(rnorm(200),rnorm(200, mean=.8)))
df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B")))
ggplot(df1, aes(x=rating, fill=cond)) +
geom_histogram(binwidth=.5, position="dodge") +
geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
show_guide=TRUE) +
labs(fill='Stochastic',linetype='Deterministic')
Edit: added image
Cheers,
Ryan
One workaround is to change the order of geom_histogram() and geom_vline(). Then add another geom_vline() without aes(), just giving xintercept= and linetype=. This will not remove lines but will hide them under the color legend entries.
ggplot(data=df1, aes(x=rating, fill=cond)) +
geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
show_guide=TRUE) +
geom_histogram(binwidth=.5, position="dodge") +
geom_vline(xintercep=df2$x,linetype=c(1,3))+
labs(fill='Stochastic',linetype='Deterministic')

Resources