How to scale a bar plot using ggplot2? - r

I want my bar plot to take up less space, currently there is a lot of white space which is not neccessary. I would like the bins to be a lot closer than they currently are. And since there are only two categories in the X axis I do not see why there is so much space between them. - increasing the bin width would make white space go away, but then the bins become unnaturally large.
Code:
# Creates plot to show response-rate
hubspot_ordering %>%
ggplot(aes(x = Replied)) +
geom_bar(color = "black",
fill = "steelblue",
stat = "count",
width = 0.1,
position = position_dodge(0.9)) +
xlab("Kommuners respons på e-post og oppringing") +
scale_x_discrete(labels = c("Besvart",
"Ingen respons")) +
ylab("Antall kommuner") +
theme(element_line(size = 0.5)) +
theme_bw() -> plot_response_rate
Output:

You can change the aspect.ratio in your theme using the following code:
df <- data.frame(x=factor(LETTERS[1:2]), y=sample(1:100, 2))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.1)) +
geom_bar(stat="identity", position="identity") +
theme(aspect.ratio = 2/1) +
labs(x="", y="")
Output aspect.ratio=2:
Output aspect.ratio=1:

Related

Ggplot2 fill by one condition, but shade by another

I am having trouble with ggplot2. I am trying to plot this boxplot that has several boxes for every x-axis value. Each set of boxplots is divided into two sets, features, and resolutions, with every feature set having boxes for every resolution. My issue is that I want to color(and fill) by feature set, but have different shades of that color for every resolution. I have only been able to color by feature and fill by resolution so far, and this is my code:
df %>%
ggplot( aes(x=partition, y=value, fill=resolution,color=feature_set)) +
scale_color_manual(values = c('black','blue3','darkred')) +
geom_boxplot(aes(color=feature_set,fill=resolution),lwd=0.7) +
scale_fill_brewer(palette = "Pastel1") +
theme(
legend.position="right",
plot.title = element_text(size=11)
) +
theme_bw() +
geom_text(data = df,aes(y=1.1,label=max_clusters,color=feature_set),size = 2.5,
position = position_dodge(width = 0.85),check_overlap = TRUE) +
xlab('label') +
ylab('label') +
ggtitle("title") +
ylim(0,1.1)
Thank you so much in advance.

How can I make sure that plots are not cutoff when saving as a jpeg in r?

I have created a combined plot with ggarrange which looks good in the plot window, however when I save it as a jpeg at 300dpi A4 size I notice that the legend of plot A and the boxplot titles for plot B get cut off, as if they are not being resized appropriately for the size I specified. I've also noticed that the size of the font in the legend is larger than what it looks on R prior to saving.
Saved image as jpeg
Screenshot from R studio
These are the codes for my plots:
Plot A
taxaplot2 <- ggplot(group, aes(x = variable, y = value, fill = Species)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(breaks = Species2, values = Cb64k) +
scale_y_continuous(labels = percent_format()) +
theme(legend.position = "bottom", text=element_text(size=11),
axis.text.x = element_text(angle=0, vjust=1)) +
guides(fill = guide_legend(ncol=5)) +
facet_grid(cols=vars(group), scales = "free_x", space = "free_x") +
ggtitle(opt$gtitle) +
xlab("Patient ID") + ylab("Relative Abundance")
Plot B
pl <- ggplot(df.humann2.sub.all4, aes(x=as.factor(PEDIS), y=PathAbundance, fill=as.factor(PEDIS)))
pl <- pl + geom_boxplot()
pl <- pl + facet_wrap(~ variable)
Combining plots and export
combined_plot <- ggarrange(taxachart2, pl, heights = c(4,1.75), labels=c("A","B"), ncol=1, nrow=2)
jpeg("test2.jpeg", units = "in", width = 8, height = 11, res=300)
combined_plot
dev.off()
How can I ensure that the saved image will look exactly the same as the screenshot above?

single level variable in ggplot2::geom_tile

I've got a data frame with three variables, location, price, and varname.
I'd like to use ggplot2's geom_tile to make a heat map of sorts. This plot almost looks like a bar chart, but I prefer geom_tile because I like the values, big or small, to be allocated the same amount of physical space on the plot. My code almost gets me there.
The first problem's that I can't format the plot so to get rid of all the white space to the left and right of my pseudo-bar. The second problem's that I can't remove the Price legend below the plot, because I'd like Price only to feature in the legend above the plot.
Thanks for any help!
Starting point (df):
df <- data.frame(location=c("AZ","MO","ID","MI"),price=c(1380.45677,1745.1245,12.45652,1630.65341),varname=c("price","price","price","price"))
Current code:
library(ggplot2)
ggplot(df, aes(varname,location, width=.2)) + geom_tile(aes(fill = price),colour = "white") + geom_text(aes(label = round(price, 3))) +
scale_fill_gradient(low = "ivory1", high = "green") +
theme_classic() + labs(x = "", y = "") + theme(legend.position = "none") + ggtitle("Price")
Don't set the width to 0.2.
Use theme to disable the labels and ticks.
You might want to use coord_equal to get nice proportions (i.e. squares). expand = FALSE gets rid of all white space.
.
ggplot(df, aes(varname, location)) +
geom_tile(aes(fill = price), colour = "white") +
geom_text(aes(label = round(price, 3))) +
scale_fill_gradient(low = "ivory1", high = "green") +
theme_classic() + labs(x = "", y = "") +
theme(legend.position = "none", axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
ggtitle("Price") +
coord_equal(expand = FALSE)

Remove spaces between bars in barplot 2 factors

I have code following:
df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),roi=rep(1:10,10),area=runif(1‌​00, 5.0, 7.5))
df$time <- factor(df$time, levels=rev(levels(df$time)))
ggplot(data=df, aes(y=factor(roi), x=time, fill = area)) + theme_minimal() + coord_flip() + geom_tile(colour = "white", width = 0.9, height = 0.5) + scale_fill_gradient(low="blue",high="red")
How can I remove the spaces between bars of 10 rois (I mean the spaces between rois) and make 10 bars of 10 rois stay continuously for each value of time. Thank you very much for any help.
Instead of the height and width arguments to geom_tile, use coord_fixed(ratio=r) to control the aspect ratio of
the tiles (where r is the ratio). I've used r=0.5 in the example below.
Switch the x and y columns so that you get roi on the x-axis without needing coord_flip() (you can't use both coord_flip() and coord_fixed() at the same time).
Set colour=NA so that there won't be a space between each tile within a given row.
Set height=0.9 to create a space between each row of tiles.
ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) +
theme_minimal() +
coord_fixed(ratio=0.5) +
geom_tile(colour = NA, width = 1, height = 0.9) +
scale_fill_gradient(low="blue",high="red")
Not sure if this is what you want, but seems like you could just change the height in geom_tile?
ggplot(data=df, aes(y=factor(roi), x=time, fill = area)) +
theme_minimal() +
coord_flip() +
geom_tile(colour = "white", width = 0.9, height = 1.3) +
scale_fill_gradient(low="blue",high="red")

width and gap of geom_bar (ggplot2)

I want to make bar plots using ggplot. After plotting when i do export > copy to clipboard and then try to adjust the size of my plot, the gaps between the bars change as I change the size of the plot (the gaps between the bars change its position).
I hope I am clear on this. I have used the following code:
ggplot(df, aes(Day, Mean)) +
geom_histogram(stat = "identity", position = "stack") +
theme(axis.text = element_text(size=12, colour = 'black')) +
ylim(0, 50) + xlim(0, 365)
I have tried using both geom_bar and geom_histogram, both seems to give the same plot. And the gaps between the bars changes as I adjust the size.
P.S. I have to plot 365 bars, each one representing one day of a year.
Feel free to edit the question, if you think that i am unclear.
This is the plot produced with the code. You can clearly see the uneven gaps in between.
here is the data:
data
Edit
data
df <- data.frame(Mean=c(0,0,0,0,0,0,0.027272727,0,0.409090909,0.009090909,0,0,0,0,0,0.054545455,0.036363636,0.118181818,0.327272727,0.254545455,0,0,1.609090909,1.636363636,0,0.181818182,1.2,0.090909091,0.409090909,0.418181818,1.018181818,0.409090909,0.127272727,0.072727273,0.054545455,1.2,0.127272727,0.290909091,0,0.518181818,0.254545455,0.454545455,1.545454545,1.1,1.763636364,0,0.136363636,0.7,0.445454545,1.954545455,0.018181818,0,2.618181818,0,0,3.518181818,7.645454545,2.709090909,5.909090909,0.9,1.109090909,2.354545455,0.418181818,0.272727273,0,0,1.636363636,0,2.927272727,0.472727273,1,0,2.109090909,0.490909091,0.827272727,2.663636364,4.8,0.554545455,6.3,3.936363636,2.218181818,0.045454545,0,7.109090909,0,3.745454545,3.009090909,8.818181818,6,21.99090909,2.845454545,3.918181818,1.4,12.32727273,1.136363636,4.345454545,1.018181818,2.927272727,12.53636364,2.618181818,0.709090909,5.645454545,5.345454545,3.181818182,2.681818182,13.96363636,3.990909091,9.9,12.54545455,8.545454545,11.43636364,6.281818182,1.836363636,11.4,4.827272727,16.14545455,3.581818182,1.972727273,3.4,4.472727273,18.86363636,5,11.4,5.790909091,3.745454545,1.072727273,2.581818182,5.063636364,12.42727273,9.2,10.85454545,15.18181818,5.963636364,22.53636364,5.027272727,7,4.572727273,7.190909091,15.42727273,7.3,23.48181818,30.87272727,19.62727273,6.463636364,16.20909091,9.509090909,5.1,8.127272727,5.890909091,11.84545455,10.14545455,4.518181818,15.23636364,22.41818182,21.62727273,7.245454545,19.56363636,33.94545455,26.98181818,9.027272727,11.28181818,20.44545455,27.52727273,23.25454545,28.77272727,20.04545455,30.68181818,28.32727273,12.38181818,13.54545455,18.17272727,12.97272727,38.14545455,20.2,14.30909091,39.44545455,34.4,34.49090909,23.32727273,29.37272727,50.68181818,23.2,16.28,35.02,49,18.86,30.96,37.83,33.01,44.31,25.51,33.76,15.05,24.8,8.99,15.72,41.31,41.47,28.12,44.22,30.63,37.35,15.72,12.86,21.89,18.02,6.32,4.73,24.16,29.12,11.58,24.25,22.69,15.7,24.36,20.05,17.19,26.71,17.84,16.53,9.3,5.11,10.97,19.95,5.65,29.88,34.95,24.14,32.09,9.85,17.49,13.72,7.97,26.21,24.9,26.45,14.1,9.52,18.64,13.43,15.17,26.61,9.84,24.9,16.42,19.58,17.58,22.96,39.61,22.83,15.49,23.64,16.71,3.96,10.17,19.04,28.42,16.64,4.95,9.73,13.45,11.67,8.02,8.71,8.31,17.65,8.41,7.19,11.94,5.15,5.54,5.21,0.88,0.96,6.18,9.46,10.24,17.29,8.95,16.51,6.31,11.4,5.05,8.28,0.26,0,6.19,1.02,0.99,0.94,1.87,0,0.21,3.32,3.33,7.82,2.65,5.21,0.49,1.59,0.05,2.25,0,0,1.09,0.42,0,0.05,0.02,0,0.18,0,0.02,0.05,0.09,0.01,1.01,0,0,2.38,0.42,0.65,0,0,0,0.4,0,0,0,0,0.18,0,0,0,0,0,0.63,0,0,0,0.1,0,0,0,0,0,1.35,0,0,0,0,0,1.62,0.2,0,0,0,1.3,0,0,0,0,0.89,4.55,0), Day=seq(1, 365, 1))
Setting the width to a small value and specifying the color gives me the desired result with gaps between all bars:
ggplot(df, aes(x = Day, y = Mean)) +
geom_bar(stat = "identity", width = 0.1, color = "black") +
theme_bw() +
theme(axis.text = element_text(size = 12))
the resulting plot:
If you want no gaps, use width = 1:
ggplot(df, aes(x = Day, y = Mean)) +
geom_bar(stat = "identity", width = 1) +
theme_bw(base_size = 12)
the resulting plot:
Same problem but with side-by-side bars will be resolved using the width within the position element:
geom_bar(width=0.1, position = position_dodge(width=0.2))

Resources