I just started using 'ggplot2', and am running into some problems with the graphical usability.
I wanted to do a simple regression biplot. However, I am not quite convinced by the themes offered by 'ggplot2' and 'ggthemes'.
My code thus far is as follows:
ggplot(data, aes(APE.15N, APE.13C)) +
geom_point(size=3) +
geom_smooth(method="lm", se=F, col="black") +
theme_light(base_size = 20) +
annotate("text", x=.9, y=1.35, label="R²=0.3192, p<0.001", size=6.5) +
coord_cartesian(xlim = c(.25, 1.1), ylim = c(1.05, 2.55)) +
ylab(expression(paste('APE '^{13}, "C", sep = ""))) +
xlab(expression(paste('APE '^{15}, "N", sep = "")))
...which gives me the following plot:
ouput from R with ggplot2
Now, I would like to increase the axis-line thickness as well as the tick thickness to at least 2 points, add minor ticks, get rid of the background grid, and change the axis colour to black.
I just can't figure out how...
I would imagine the result as in the following graph:
example graph
Thank you,
your help is very much appreciated
To increase the axis-line thickness and change the color to black:
axis.line = element_line(colour = 'black', size = 2)
To increase the tick thickness:
axis.ticks = element_line(colour = "black", size = 2)
To add minor ticks:
Minor ticks are not currently an option of ggplot2. There are many other stackoverflow questions about minor ticks that I would suggest looking at. You can try adding minor_breaks in scale_x_continuous, but that would require a knowing the actual minor ticks you want.
To remove the background grid:
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
Related
I want to add a vertical minor grid between two major grids in a boxplot with discrete x variables in ggplot2.
This is the sample:
boxplot <- ggplot(data = mtcars ,aes(x = as.factor(cyl),y=wt, fill=as.factor(am))) + geom_boxplot()
boxplot
As seen from the visualization, it can be unclear which box belongs to which x label because the major vertical grid is separating the two boxes at the same x-variable (it may not be an issue here, but it does become problematic when there are many x variables and narrow boxes). Therefore, I am thinking of adding a minor grid in the middle of each major grid. I tried using the "minor grid.x" in ggplot2, shown below, but I could not see any added lines.
boxplot + theme(panel.grid.minor.x = element_line(color="black"))
I've looked over related posts on setting gridlines, but it seems that they are focused on continuous x variables, and is not applicable to box plots.
Thank you in advance.
Update thanks to #Allan Cameron:
ggplot(data = mtcars ,aes(x =factor(cyl),y=wt, fill=as.factor(am))) +
geom_boxplot() +
geom_vline(xintercept = c(1.5, 2.5),linetype="dashed",colour="green",size=1)
First answer:
Are you looking for such a solution:
library(ggplot2)
ggplot(data = mtcars ,aes(x =factor(cyl),y=wt, fill=as.factor(am))) +
geom_boxplot() +
geom_vline(aes(xintercept=1.5),linetype="dashed",colour="green",size=1)+
geom_vline(aes(xintercept=2.5),linetype="dashed",colour="green",size=1)
You can try this and modify the values of the theme and see what works better for you:
boxplot +
theme(
panel.grid.major.y = element_line(color = "blue",
size = 0.5,
linetype = 2),
panel.grid.minor.y = element_line(color = "red",
size = 0.25,
linetype = 1),
panel.grid.major.x = element_line(color = "green",
size = 0.5,
linetype = 3)
)
I've created a correlation matrix:
cor_matrix = cor(qual_colleges_all_data_clean[ ,c(4,5,8,9,10,12,13,14,16,20,21,22,23,25)], method='pearson',use='complete.obs')
ggcorrplot(cor_matrix, hc.order = TRUE, method ='circle', type='lower', colors = c("darkblue", "white", "red")) +
labs(title = "Correlation Matrix:", subtitle = "Select Variables") +
theme_minimal() +
theme(
legend.key.width = unit(0.6, "cm"),
legend.key.height = unit(1.3, "cm"),
axis.text.x = element_text(angle = 45) #to put x-axis labels at 45 angles
)
Here's what it looks like:
Initially the x-axis labels were all horizontal and thus were a jumbled mess, so I angled them. But as you can see, they're overlapping the plot. The problem is that I need to preserve theme_minimal() if at all possible. I've tried every theme() adjustment I could find online, but I cannot make anything work. So, I have three questions:
How can adjust the x-axis headings while preserving theme_minimal()?
How can I widen the plot while preserving theme_minimal()? My hope is that will space the circles a bit more suitably.
How can I eliminate the Var2 and Var1 axis labels while preserving theme_minimal()?
I realize these are basic questions, so I really appreciate you taking the time to help me. Thanks in advance!
I have a two-part waffle plot with a lot of little squares, and a legend that contains two squares. I would like to make the sizes of all of these squares to be the same.
There was an issue opened on GitHub about this, and the repo owner said that since waffle() returns a ggplot2 object, we can use guide() to do this.
I tried searching on documentation to do this and came up with
library(waffle)
phrase_count = 17345/10000
all_count = (22784085 - phrase_count)/10000
my_waffle = waffle(c("All"=all_count, "Phrases"=phrase_count),
rows=43,
size=0.6,
colors=c("#969696", "pink", "white"),
flip=TRUE)
my_waffle + guides(colour=guide_legend(override.aes = list(size=0.6)))
but this doesn't affect the size of the legend. I've seen people use color, colour, or shape, but none of these arguments work for me.
How do I get the size of squares in the legend to be the same as the size of the squares in the plot itself?
Try setting theme() for your legend key as the waffle object is from ggplot2 nature as mentioned in comments by #Waldi:
#Code
my_waffle <- my_waffle + theme(legend.key.size = unit(3, "mm"))
Output:
Or maybe this:
#Code 2
my_waffle + theme(legend.key.height = unit(0.2, "cm"),
legend.key.width = unit(0.3, "cm"))
Output:
I'm having some issues with ggplot2 and y axis tick marks - if someone can provide any input I'd really appreciate it.
I'm trying to create a 'stacked' plot with independent y axis for publication I'm working on. The idea is to have N plots stacked with a common X axis but distinct Y axes for each subplot while making it seem like a single contiguous plot.
I would like inverted tick marks on the x axis (for the bottom most subplot) and all the y axes. Problem is while the tick marks show up on the yaxis breifly in the plot generation they seem to be overwritten at the last stage and become invisible. Its such a minor thing and I could just leave them off but I'd really like to know what is going on for my own sanity...
Below is some sample code that should reproduce the problem and here is an imgur link highlighting the plot style and missing tick marks.
On a tangent, if anyone knows how to customize the axis.line.y.right / axis.line.x.top without using a dummy 'second axis' let me know (it seems a very verbose way of doing something that should be simple).
Thanks for your help
ylim=c(-5,5)
xlim=c(3,12)
ybreaks=c(-2,2)
base <- ggplot() +
theme_bw() +
scale_y_continuous(limits=ylim, breaks=ybreaks) +
scale_x_continuous(limits=xlim) +
labs(x="", y="") +
theme( panel.grid.major=element_line("gray78",0.5,1),
panel.border=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_text(margin=unit(c(0,3,0,0), "mm")))
bottom <- base + xlab("xlab") +
theme( axis.ticks.length.y=unit(-2,"mm"),
axis.ticks.length.x=unit(-2,"mm"),
axis.line.y=element_line(),
axis.line.x=element_line(),
plot.margin=unit(c(0,5,5,5),"mm"),
axis.text.x=element_text(margin=unit(c(3,0,0,0), "mm")))
middle <- base +
theme( axis.ticks.length.y=unit(-2,"mm"),
axis.ticks.length.x=unit(0,"mm"),
axis.line.y=element_line(),
axis.line.x=element_line(linetype=3),
plot.margin=unit(c(0,5,0,5),"mm"),
axis.title=element_blank())
top <- base +
theme( axis.ticks.length.y=unit(-2,"mm"),
axis.ticks.length.x=unit(0,"mm"),
axis.line.y=element_line(),
axis.line.x=element_line(linetype=3),
plot.margin=unit(c(5,5,0,5),"mm"),
axis.title=element_blank())
ggarrange(top,middle,bottom,ncol=1)
The easiest and more ggplot-y way would be to use facets.
I recommend using labs(x=NULL, y=NULL), because using = '' actually is drawing something.
I am removing the facet strips in the plot, but I generally think your graph may be slightly less confusing when you keep the labels and also keep a bit of distance between those graphs.
In order to add the dashed lines between your facets, you could simply add annotations, e.g., with annotate(geom = 'segment')
library(ggplot2)
ylim=c(-5,5)
xlim=c(3,12)
ybreaks=c(-2,2)
ggplot(data.frame(facet = letters[1:3])) +
theme_bw() +
annotate(geom = 'segment', x = -Inf, xend = Inf, y = -Inf, yend = -Inf, linetype = 3)+
scale_y_continuous(limits=ylim, breaks=ybreaks) +
scale_x_continuous(limits=xlim) +
labs(x=NULL, y=NULL) +
theme( panel.grid.major=element_line("gray78",0.5,1),
panel.border=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_text(margin=unit(c(0,3,0,0), "mm")),
panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.text = element_blank(),axis.line.y=element_line(),
axis.line.x=element_line(),
axis.ticks.length.y=unit(-2,"mm"),
axis.ticks.length.x=unit(-2,"mm"),
plot.margin=unit(c(b = 5, t = 5, r = 5, l = 5),"mm")) +
facet_wrap(~facet, nrow = 3)
Created on 2020-02-22 by the reprex package (v0.3.0)
I am attempting to make a facet_wrap bar_graph with error bars (se) that clearly shows three different categorical variables (Treatment, Horizon, Enzyme) with one response variable (AbundChangetoAvgCtl). Below is the code for some dummy data followed by the ggplot code I have so far. The graphs I've made can be see at this link:
bargraph figures
Enzyme <- c("Arabinosides","Arabinosides","Arabinosides","Arabinosides","Arabinosides","Arabinosides","Cellulose","Cellulose","Cellulose","Cellulose","Cellulose","Cellulose","Chitin","Chitin","Chitin","Chitin","Chitin","Chitin","Lignin","Lignin","Lignin","Lignin","Lignin","Lignin")
Treatment <- c("Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low")
Horizon <- c("Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min")
AbundChangetoAvgCtl <- rnorm(24,mean=0,sd=1)
se <- rnorm(24, mean=0.5, sd=0.25)
notrans_noctl_enz_toCtl_summary <- data.frame(Enzyme,Treatment,Horizon,AbundChangetoAvgCtl,se)
ggplot(notrans_noctl_enz_toCtl_summary, aes(x=Horizon, y=AbundChangetoAvgCtl, fill=Horizon, alpha=Treatment)) +
geom_bar(position=position_dodge(), colour="black", stat="identity", aes(fill=Horizon)) +
geom_errorbar(aes(ymin=AbundChangetoAvgCtl-se, ymax=AbundChangetoAvgCtl+se),
width=.2,
position=position_dodge(.9)) +
scale_fill_brewer(palette = "Set1") + theme_bw() +
geom_hline(yintercept=0) +
labs(y = "Rel Gene Abundance Change / Control", x="") +
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
strip.text.x = element_text(size=20),
plot.title = element_text(size=22, vjust=2, face="bold"),
axis.title.y = element_text(size=18),
legend.key.size = unit(.75, "in"),
legend.text = element_text(size = 15),
legend.title = element_text(size = 18)) +
facet_wrap(~Enzyme, scales="free")
(figure 1)
So this is close to what I want, however for some reason, the "alpha=Treatment" call in ggplot causes my errorbars to fade (which I don't want) as well as the bar_fill (which I do want). I've tried moving the "alpha=Treatment" to the geom_bar call, as well as adding "alpha=1" to geom_bar, but when I do that, the error bars all move to a single location and overlap (figure 2).
I initially wanted to cluster the bars within facet_wrap, but found the alpha option on this site, which seems to accomplish what I'm looking for as well. Any help would be appreciated. If there is a better way to represent all of this, those ideas are welcome as well.
Also, if there is a way to condense and clarify my legend, that would be extra bonus!
Thanks in advance for your help!
Mike
You need to assign Treatment to the group option in the ggplot() command and then move the alpha=Treatment option to the geom_bar() command. Then the alpha value of geom_errorbar won't be affected by the global option and will be black. Like this:
ggplot(notrans_noctl_enz_toCtl_summary, aes(x=Horizon, y=AbundChangetoAvgCtl, fill=Horizon, group = Treatment)) +
geom_bar(position=position_dodge(), colour="black", stat="identity", aes(fill=Horizon, alpha = Treatment))
Also, I would check whether setting alpha=Treatment corresponds to more transparent as being equivalent to low treatment and less transparent to high treatment. At least that would be my intuitive understanding, without any background on the research design or data.
For information about formatting legends, see here.