Adjusting graph bar height with ggplot2 [duplicate] - r

This question already has an answer here:
Losing the grey margin padding in a ggplot
(1 answer)
Closed 7 years ago.
df2 <- iris[c(5,1)]
df3 <- aggregate(df2$Sepal.Length, list(df2$Species), mean)
names(df3) <- c("x","y")
ggplot(df3, aes(x,y)) +
geom_bar(aes(fill=x),stat="identity") +
theme(axis.ticks=element_blank(), axis.text.x=element_blank())
I have successfully removed axis tick marks and labels from this plot. I am trying to get rid of the blank grey space underneath the bars. Zero should be the lower bound for the chart. I've been searching unsuccessfully for an adjustment function to either pull the bars down or to cut off the bottom grey portion. The hope is that ggplots are not hardwired with the extra space underneath.

Add the following elements to your code (inspired by this Q & A):
theme_classic()
scale_x_discrete(expand=c(0,0))
scale_y_continuous(expand=c(0,0))
Instead of theme_classic(), you can also use theme_bw() which will add horizontal and vertical lines to the plot.
You code should then look like this:
ggplot(df3, aes(x,y)) +
geom_bar(aes(fill=x),stat="identity") +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
theme_classic() +
theme(axis.ticks=element_blank(), axis.text.x=element_blank())
this gives:

Related

Adding a "//" on the x-axis to remove whitespace in one side of the ggplot panel plot

I'm hoping if there's a way to remove whitespace in one side of the panel plot (created by facet_wrap) by adding "//" on the x-axis. Below is sample data and code:
df <- data.frame(
condition = c("cond1","cond2","cond3"),
measure = c("type1","type2"),
value = rep(NA, 6)
)
# all type 1 measure values are between -0.5 and 0.5
# all type 2 measure values are between 0.5 and 2
df[df$measure=="type1",]$value <- runif(3, min=-0.5, max=0.5)
df[df$measure=="type2",]$value <- runif(3, min= 1.5, max=2.0)
# both panels should have same axis tick intervals
custom_breaks = function(x){
seq(round(min(x), 2), round(max(x), 2), 0.2)
}
# create a panel plot with vertical line at y=0 for both panels
ggplot(df, aes(x=condition, y=value, color=measure)) +
geom_point() +
geom_hline(aes(yintercept=0), color="grey") +
scale_y_continuous(breaks=custom_breaks) +
facet_wrap(~measure, scales="free_x") +
coord_flip() +
theme_bw() +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())
This code returns the below plot:
Because the values for type 2 (right panel) are far off from zero, adding a vertical line at y=0 results in lots of whitespace. I'm wondering if there's a way to put a "//" on the x-axis on the right panel after 0 and going straight to 1.5 so there aren't tons of wasted white space. Any help would be greatly appreciated!
Broken axes are generally discouraged because they can lead to misleading visualizations, so this is intentionally not implemented in ggplot2 (as answered by Hadley Wickham himself).
My preferred solutions for something like this are (a) facetting (which you are already doing) or (b) log transormation of the axis - but only if it makes sense for the given data.
Take this barchart for example (source / link to image): Since there is valuable information in the outliers (red circle and arrows) both log transformation and broken axes would distort the representation of reality. The package library(ggforce) has an implementation for such zoom facets with the facet_zoom() function.
Your scales = "free_x" is working just fine - the issue is that your geom_hline putting a line at 0 is included in both facets. Here's a way to include it only on the first facet.
ggplot(df, aes(x=condition, y=value, color=measure)) +
geom_point() +
geom_hline(data = data.frame(measure = "type1"), aes(yintercept=0), color="grey") +
scale_y_continuous(breaks=custom_breaks) +
facet_wrap(~measure, scales="free_x") +
coord_flip() +
theme_bw() +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())

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 place a text on a free position in a ggplot2 plot [duplicate]

This question already has answers here:
How to position annotate text in the blank area of facet ggplot
(2 answers)
Closed 4 years ago.
If I use the following code
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~cyl, ncol = 2)
p
I obviously get a chart with one blank facet in the SE corner (resulting chart). I'd like to place some text into this blank facet providing e.g. an explanation or any other information pertinent to the chart. I haven't yet found a solution, all solutions place the text in the filled facets.
Thanks for your help!
Ulrich
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~cyl, ncol = 2)
p
library(grid)
grid.text("your text", x = 0.75, y = 0.25)
see the answer that i got here

Combined box-violin plot not aligned [duplicate]

This question already has an answer here:
Align violin plots with dodged box plots
(1 answer)
Closed 7 years ago.
I want to graph a distribution along two dimensions using a violinplot with a boxplot in it. The result can be really fascinating, but only when done right.
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
plot <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin() + geom_boxplot(width=0.1) + theme(legend.position="none")
ggsave(filename="Violinboxplot.png", plot, height=6, width=4)
This is however what I get:
The boxplots are aligned along the axis belonging to the factor. How can I shift them to be in the center of the violinplots?
There is an answer to this question here:
how to align violin plots with boxplots
You can use the position argument to shift the graph elements as needed:
dodge <- position_dodge(width = 0.5)
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin(position = dodge) +
geom_boxplot(width=.1, position = dodge) +
theme(legend.position="none")

How to keep or remove grey margin for geom_tile on all facets? [duplicate]

This question already has an answer here:
Margin adjustments when using ggplot's geom_tile()
(1 answer)
Closed 9 years ago.
You can see on one group there is horizontal grey margin, on the other there is none.
How can I make the margin consistent across the facets?
expand.grid(x=1:3, y=1:3)
a<-expand.grid(x=1:3, y=1:3)
a$value=rnorm(9)
a$group=1
b<-expand.grid(x=3, y=1:3)
b$value=rnorm(3)
b$group=2
c<-rbind(a,b)
ggplot(c, aes(x=factor(x), y=factor(y), fill=value)) +
geom_tile() + facet_grid(.~group, scale="free_x", space="free_x")
You should add expand=c(0,0) inside the scale_x_discrete() and scale_y_discrete() to remove grey area.
+scale_x_discrete(expand=c(0,0))+
scale_y_discrete(expand=c(0,0))

Resources