ggplot2 panel graph is slightly off compared to others - r

I am aligning line graphs in a panel. First one is slightly off compared to the other two (see figure). I have similar problems with the same kind of graph in other cases (in the other cases, the graph in question is the second one, and in all cases it is labeled as "study 2). I am using ggplot 2:
Setting up data & theme:
library(ggplot2)
# common theme used by all plots
theme1 <- theme(plot.title = element_text(hjust = 0.5, size = 20, face = "italic"),
legend.position = c(0.5,0.2),
legend.text=element_text(size=10, face="bold", color="black"),
legend.title = element_blank(),
legend.key.size = unit(2.5, "line"),
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, face = "bold", size=15, color="black"),
panel.background = element_rect(fill = "white"),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0.2, "cm"),
axis.text.y = element_text(size = 18, colour="black"),
panel.border = element_rect(colour = "black", fill=NA, size=3),
panel.grid.major = element_line(size=0.2, colour = "gray", linetype="solid"),
panel.grid.minor.y =element_line(size=0.2, colour = "gray", linetype="solid"),
aspect.ratio = 4,
plot.margin = unit(c(0,0,0,2),"cm"))
# data for first plot
N300 <- data.frame(evaluation=rep(c("Low\nSalary\n($20k)", "High\nSalary\n($50k)"),
each = 2),
values =c(4.56, 4.20, 5.12, 4.55),
Condition =rep(c("High Ratio (200:1)", "Low Ratio (50:1)")))
# data for second plot
dfie2 <- data.frame(values = c(3.86, 3.94, 3.64),
condition = c("Baseline (300:1)", "CEO Lower (100:1)", "Median higher (100:1)"))
# data for third plot
dfie3 <- data.frame(evaluation=rep(c("Joint", "Separate"), each = 3),
values = c(5.03, 4.24, 4.43, 5.05, 5.21, 4.97),
Condition = rep(c("Baseline (400:1)", "CEO Lower (160:1)", "Median Higher (160:1)")))
Plot:
# first plot
Oldstudy <- ggplot(data=N300, aes(x=Condition, y=values, group=evaluation, label=values)) +
ggtitle("Study 2")+
geom_line(aes(linetype=evaluation), size=1.5) +
geom_point(shape=20, size=5) +
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
scale_linetype_manual(values=c("solid", "dotdash")) +
theme1
# second plot
France <- ggplot(data=dfie2, aes(x=condition, y=values, group=1)) +
ggtitle("Study 3")+
geom_line(size=1.5)+
#geom_text(aes(label=values), vjust=2, fontface="bold", size=5)+
geom_point(shape=20, size=5)+
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
theme1
# third plot
JS <- ggplot(data=dfie3, aes(x=Condition, y=values, group=evaluation)) +
ggtitle("Study 4")+
geom_line(aes(linetype=evaluation), size=1.5)+
geom_point(shape=20, size=5)+
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
scale_linetype_manual(values=c("solid", "dotted"))+
theme1
#### combine graphs ####
library(grid)
library(gridExtra)
grid.arrange(Oldstudy, France, JS, nrow = 1,
top = textGrob("Support for Lowering CEO compensation",
gp = gpar(fontface = "bold", fontsize = 20)
))
This is the plot that the code generates:

It looks like the three plots are being aligned by their middle points, which is causing the first one to look misaligned as it has shorter X-axis labels than the other two.
Instead of using the grid.arrange() function, you could try using the plot_grid() function of the cowplot package instead, which should align the plots by their axes instead of centring each one.

(Please note that I've done a considerable amount of cleaning / steamlining on the code included in your question. You are, of course, free to roll back the edit if you feel it misrepresents your problem, but I second what #camille mentioned in her comment that your original code had too many unnecessary details. While terse code can be hard to read, & comments help a reader understand what a piece of code is trying to do, peppering EVERY line with comments is generally a step too far.)
library(cowplot)
# just the plots, arranged horizontally
joint.plot <- plot_grid(Oldstudy, France, JS, nrow = 1, align = "h", axis = "tb")
# define joint title
plot.title <- ggdraw() +
draw_label("Support for Lowering CEO compensation",
fontface = 'bold', size = 20)
# combine the title & the joint plots vertically
plot_grid(plot.title, joint.plot,
ncol = 1,
rel_heights = c(0.1, 1)) # adjust relative height of title vs plot as needed

Related

Adjust grid lines in ggplot+geom_tile (heatmap) or geom_raster

This heatmap has a grid builtin, which I am failing to find the way to customize.
I want to preserve horizontal lines in the grid, if possible increase thickness, and disable vertical lines. Each row should look as a continuous time-serie where data is present and blank where it is not.
Either adding vertical/horizontal lines on-top would possibly cover some data, because of that grid lines, or controlled gaps between tiny rectangles, is preferable.
Alternativelly, geom_raster doesn't shows any grid at all. With which I would need to add the horizontal lines of the grid.
I tried changing linetype, the geom_tile argument, which does seem to change the type or allow to fully disable it with linetype=0, fully disabling the grid, but it wouldn't allow to preserve horizontal grid-lines. I didn't saw any changes by modifying the size argument.
This is the code generating the plot as above:
ggplot( DF, aes( x=rows, y=name, fill = value) ) +
#geom_raster( ) +
geom_tile( colour = 'white' ) +
scale_fill_gradient(low="steelblue", high="black",
na.value = "white")+
theme_minimal() +
theme(
legend.position = "none",
plot.margin=margin(grid::unit(0, "cm")),
#line = element_blank(),
#panel.grid = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = element_blank(),
#panel.grid = element_line(color="black"),
#panel.grid.minor = element_blank(),
plot.caption = element_text(hjust=0, size=8, face = "italic"),
plot.subtitle = element_text(hjust=0, size=8),
plot.title = element_text(hjust=0, size=12, face="bold")) +
labs( x = "", y = "",
#caption= "FUENTE: propia",
fill = "Legend Title",
#subtitle = "Spaces without any data (missing, filtered, etc)",
title = "Time GAPs"
)
I tried to attach DF %>% dput but I get Body is limited to 30000 characters; you entered 203304. If anyone is familiar with a similar Dataset, please advise.
Additionally,
There are 2 gaps at left&right of the plot area, one is seen inbetween the y-axis, and at the right you can see the X-axis outbounding, and are not controlled by a plot.margin argument.
I would want to set the grid to a thicker line when month changes.
The following data set has the same names and essential structure as your own, and will suffice for an example:
set.seed(1)
DF <- data.frame(
name = rep(replicate(35, paste0(sample(0:9, 10, T), collapse = "")), 100),
value = runif(3500),
rows = rep(1:100, each = 35)
)
Let us recreate your plot with your own code, using the geom_raster version:
library(ggplot2)
p <- ggplot( DF, aes( x=rows, y=name, fill = value) ) +
geom_raster( ) +
scale_fill_gradient(low="steelblue", high="black",
na.value = "white") +
theme_minimal() +
theme(
legend.position = "none",
plot.margin=margin(grid::unit(0, "cm")),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = element_blank(),
plot.caption = element_text(hjust=0, size=8, face = "italic"),
plot.subtitle = element_text(hjust=0, size=8),
plot.title = element_text(hjust=0, size=12, face="bold")) +
labs( x = "", y = "", fill = "Legend Title", title = "Time GAPs")
p
The key here is to realize that discrete axes are "actually" numeric axes "under the hood", with the discrete ticks being placed at integer values, and factor level names being substituted for those integers on the axis. That means we can draw separating white lines using geom_hline, with values at 0.5, 1.5, 2.5, etc:
p + geom_hline(yintercept = 0.5 + 0:35, colour = "white", size = 1.5)
To change the thickness of the lines, simply change the size parameter.
Created on 2022-08-01 by the reprex package (v2.0.1)

Adding a single legend for two horizontal lines in ggplot

I little experience with ggplot2. I am trying to plot coverage probability and cohort size using the code below:
library("reshape2")
library("ggplot2")
library(latex2exp)
CP1 <-c(0.953,0.942,0.947,0.958)
CP2 <- c(0.937,0.952,0.955,0.957)
cohort <- c(500,1000,5000,10000)
mdata <- data.frame(rate1=CP1,rate2=CP2,cohort.size=cohort)
mydata <- melt(mdata,id='cohort.size',value.name="CP")
ggplot(mydata , aes(x=cohort.size, y=CP)) +
geom_line(size=1,aes(colour=variable)) +
geom_point( size=4, shape=0)+ coord_cartesian(ylim = c(0,1)) +
scale_x_continuous(breaks=c(500,1000,5000,10000))+
scale_color_discrete(labels = unname(TeX(c(" $\\r_1$", "$\\r_2$")))) +
geom_hline(yintercept =c(0.936,0.964) ,linetype="dashed") +
theme(legend.title = element_blank(), axis.title.x = element_text(color="#993333", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold"),
plot.title = element_text(color="#993333", size=14, face="bold"),
legend.position = c(.85, .85),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(6, 6, 6, 6),legend.text=element_text(size=20)) + xlab("Cohort Size") + ylab("Coverage Proability")+
annotate("text",
x = 8700,
y = 0.68,
label =expression(bold(paste("MN=57% \n AB=38% \n XYZ=5%" ))),parse = TRUE,size=5)
I have three questions:
1. When I run the code, I get a warning; how can I fix it.
2. There are two horizontal black dashed lines and I want to have just one legend for both to represent '95% CL'.
3. I feel the code is too much, is there a much simpler way of writing it using ggplot2 only.
Thanks!!
I can't install latex2exp. Without this package, you simply can try this and in my opinion all three questions are solved:
ggplot(mydata , aes(x=cohort.size, y=CP)) +
geom_line(size=1,aes(colour=variable)) +
geom_point( size=4, shape=0)+
geom_hline(data = data.frame(yintercept =c(0.936,0.964)),
aes(yintercept =yintercept, linetype ='95% CL')) +
scale_linetype_manual("", values = 2) +
ylim(0,1) +
annotate("text",
x = 8700,
y = 0.68,
label = paste("MN=57%\n AB=38%\n XYZ=5%" ),
size=5, fontface =2)

distance to legend text ggplot

I am trying to get a little more distance between the legend box (indicator) and the legend text. I have a code adapted from this amazing page. Here is my MWE:
library(openxlsx) # for reading in Excel data
library(dplyr) # for data manipulation
library(tidyr) # for data manipulation
library(magrittr) # for easier syntax in one or two areas
library(gridExtra) # for generating some comparison plots
library(ggplot2) # for generating the visualizations
mwedata <- data.frame(Metro=c(rep("Dayton,OH",6)))
mwedata$class <- as.character(c("Lower","Middle","Upper","Lower","Middle","Upper"))
mwedata$year <- as.numeric(c(rep(2000,3),rep(2014,3)))
mwedata$value <- as.numeric(c(0.221,0.580,0.199,0.269,0.527,0.204))
mwedata <- mwedata %>%
mutate(y_label = paste0(round(value*100, 1), "%"))
plot <- ggplot(mwedata, aes(x = class, y = value, fill = factor(year))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("#29ABE2", "#217693")) +
geom_text(aes(label = y_label), position = position_dodge(0.9),
vjust = 1.5, color = "white", family = "Georgia")
plot <- plot +
scale_y_continuous(labels = scales::percent) +
scale_x_discrete(labels = c("Lower" = "Lower Class",
"Middle" = "Middle Class", "Upper" = "Upper Class")) +
labs(title = "Distribution of Adults by Income in Dayton, OH",
subtitle = "The percentage of adults in the middle class eroded by 5.3% from 2000 to 2014. Although a small \nfraction of these individuals moved into the upper class (+0.5%), the majority of these middle class \nindividuals moved into the lower income class (+4.8%).",
caption = "Source: Pew Research Center analysis of the \n2000 decennial census and 2014 American \nCommunity Survey (IPUMS)")
plot +
theme_minimal() +
theme(axis.title = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(1,1), legend.justification = c(1,1),
legend.background = element_blank(),
legend.direction="vertical",
text = element_text(family = "Georgia"),
plot.title = element_text(size = 18, margin = margin(b = 10)),
plot.subtitle = element_text(size = 10, color = "darkslategrey", margin = margin(b = 25)),
plot.caption = element_text(size = 8, margin = margin(t = 10), color = "grey70", hjust = 0),
legend.title = element_blank(),
legend.text.align = 2)
The last line of code legend.text.align is supposed to move the text from the legend coloured boxes, but it only seem to apply for the lower of the two. See the image below. Can anyone help me?
EDIT 1:
I totally forgot to include the defined data.frame. I have now updated the MWE so it really is an WE with this line of code
mwedata <- data.frame(Metro=c(rep("Dayton,OH",6)))
Im sorry for the confusion..
This helps resolve the issue:
Remove legend.title = element_blank() and legend.text.align = 2 from theme()
Add fill = "" to labs()
Curious observation while debugging: using your original code, just changing the font family, e.g. from "Georgia" to "Open Sans", removes the discrepancy in alignment between the two labels in the legend.

adding a line break using ggplot to the x-axis text

enter image description hereI'm reading in a csv file and using ggplot to make a box plot and having issues with the size of the text on the x axis. I can't reduce the text size because I am reducing the over all image for a manuscript and this is the optimal size to make the axes visible. I'd like to have the genotype names below each box plot on the x axis, I've angled genotype names on the axis, but it still doesn't seem to be enough and looks a bit awkward. I'd like to add a line break for at least the longest genotype (4-67/Chrimson), but I don't think I am doing this right. I've tried setting new labels using in the scale_x_discrete and also in the axis.text.x but under both conditions, it either skips over that variable or ignores the command.
FYI: the data frame has two variables, group activity and genotype and genotype has 4 levels.
Thank you in advance!!
Here's my code so far..
dat5=read.csv("Time Point 120 Raw Data C-Test.csv")
plot5<- ggplot(dat5, aes(x = Genotype, y = Group.Activity, col = Genotype, fill = Genotype, ymin= -40, ymax = 50)) +
geom_boxplot(fatten = 1, lwd = .5, alpha = .6) +
# reorder so x-axis is not alphabetical
scale_x_discrete(limits=c("4-67/Chrimson","4-67/+","+/Chrimson", "+/+"))+
scale_fill_manual(values=c("#F99205", "#4ED55F", "#36A6D6", "#5752D0"))+
scale_color_manual(values=c("#F99205", "#4ED55F", "#36A6D6", "#5752D0"))+
geom_beeswarm(size = 1.5, alpha = .75, cex = 3)+
ylab("Percent Activity After Baseline Subtraction")+
theme_classic()+
theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 16, color="black"), axis.ticks.x = element_blank())+
theme(axis.text.y = element_text(size = 24, color="black"), axis.ticks.y = element_blank())+
theme(axis.title.y = element_text (size = 24, color="black"))+
theme(axis.ticks.x = element_blank())+
theme(legend.position = "none")

move legend title in ggplot2

I have been trying to shift my legend title across to be centered over the legend contents using the guide function. I've been trying to use the following code:
guides(colour=guide_legend(title.hjust = 20))
I thought of trying to make a reproducable example, but I think the reason it's not working has something to do with the above line not matching the rest of my code specifically. So here is the rest of the code I'm using in my plot:
NH4.cum <- ggplot(data=NH4_by_Date, aes(x=date, y=avg.NH4, group = CO2, colour=CO2)) +
geom_line(aes(linetype=CO2), size=1) + #line options
geom_point(size=3) + #point symbol sizes
#scale_shape_manual(values = c(1, 16)) + #manually choose symbols
theme_bw()+
theme(axis.text.x=element_text(colour="white"), #change x axis labels to white.
axis.title=element_text(size=12),
axis.title.x = element_text(color="white"), #Change x axis label colour to white
panel.border = element_blank(), #remove box boarder
axis.line.x = element_line(color="black", size = 0.5), #add x axis line
axis.line.y = element_line(color="black", size = 0.5), #add y axis line
legend.key = element_blank(), #remove grey box from around legend
legend.position = c(0.9, 0.6))+ #change legend position
geom_vline(xintercept=c(1.4,7.5), linetype="dotted", color="black")+ #put in dotted lines for season boundaries
scale_color_manual(values = c("#FF6600", "green4", "#0099FF"),
name=expression(CO[2]~concentration~(ppm))) + #manually define line colour
scale_linetype_manual(guide="none", values=c("solid", "solid", "solid")) + #manually define line types
scale_shape_manual(values = c(16, 16, 16)) + #manually choose symbols
guides(colour=guide_legend(title.hjust = 20))+
scale_y_continuous(expand = c(0, 0), limits = c(0,2200), breaks=seq(0,2200,200))+ #change x axis to intercept y axis at 0
xlab("Date")+
ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
geom_errorbar(aes(ymin = avg.NH4 - se.NH4, #set y error bars
ymax = avg.NH4 + se.NH4),
width=0.1)
I have tried doing the following instead with no luck:
guides(fill=guide_legend(title.hjust=20)
I have also adjusted the hjust value from values between -2 to 20 just to see if that made a difference but it didn't.
I'll try to attach a picture of the graph so far so you can see what I'm talking about.
I've looked through all the questions I can on stack overflow and to the best of my knowledge this is not a duplicate as it's specific to a coding error of my own somewhere.
Thank-you in advance!!
The obvious approach e.g.
theme(legend.title = element_text(hjust = .5))
didn't work for me. I wonder if it is related to this open issue in ggplot2. In any case, one manual approach would be to remove the legend title, and position a new one manually:
ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
geom_point() +
stat_smooth(se = FALSE) +
theme_bw() +
theme(legend.position = c(.85, .6),
legend.title = element_blank(),
legend.background = element_rect(fill = alpha("white", 0)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
annotate("text", x = 5, y = 27, size = 3,
label = "CO[2]~concentration~(ppm)", parse = TRUE)
Output:

Resources