Shift text in ggplot up - r

Using the this code gives the plot printed below. As you can see the percentages are printed on the border of the bars. I would like to have them above the bars. Is there a way to achieve this?
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")

Just add an arbitrary value to y.
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..) + 10,label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Or, as per Heroka's comment, use vjust, which is a better solution
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +
geom_text(aes(y = (..count..),
label = scales::percent((..count..)/sum(..count..))),
stat="bin",
colour="darkgreen", vjust = -0.5) +
theme(legend.position="none")
But as this makes things quite cramped at the top you might want to add + expand_limits(y = c(0, 60)) to give you a bit more space for the labels.

Related

How to modify my ggplot2 codes so that the bars are next to each other rather being stacked?

I have the following ggplot2 codes running in R. I need to tweak the codes so that the bars of each FY value are next to each other rather than being stacked.
My codes stand as follows:
p1 <- ggplot(dff3, aes(x=Gender, fill=FY)) + ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
p1
The plot looks like this:
rently like this:
You can use position_dodge() within geom_bar(). Here is an example using mtcars dataset:
library(tidyverse)
ggplot(mtcars, aes(x=factor(am), fill=factor(vs))) +
ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5, position = position_dodge()) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))

How to plot a lign spanning these two graphs?

Consider the following figure:
mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red")
gg = ggplot(data.frame(mpg=0), aes(x=mpg))
f = function(mpg,center) {exp(-(mpg - center)^2/(20))}
f15 = function(mpg) {f(mpg,15)}
f25 = function(mpg) {f(mpg,25)}
sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red")
multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))
As the figure is made of two independent graphs, the red horizontal lines are interrupted. Is there any way I can make it a continuous line?
It is possible that the easiest solution consists at using Adobe Illustrator (or some equivalent) to modify the figure.
Not really a solution but a work around.
Reduce the margins to stick the two graphs together
Make your line a dashed line
Remove the y axis line of the sideplot
mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,0,1,1), "cm"))
sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.line.y=element_blank(),axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,1,1,0), "cm"))
multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))

Controlling the total width of a barplot

How to get rid of all this space where the blue lines are?
Data:
data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))
data$is_repeat = factor(data$is_repeat, levels = c(0,1),
labels = c("One-time", "Repeat"))
Plot:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat = "identity", width = 0.3) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
theme_minimal()
edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.
another edit: Added data.
If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:
geom_bar(stat="identity", position="stack", width=1)
or theme(aspect.ratio=1)
And to remove the space from the end of the plot to the bars you need
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat"))
So your code looks like this:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat="identity", position="stack", width=1) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
theme_minimal()
And the output:
You can add space between bars with changing the width=1

R boxplot how is this manual fill wrong

I would like to colour my boxplot variables differently. I looked here and tried the following but the plot boxes are all the standard white colour (i have 6 factors in Type). What should i change?
library(ggplot2)
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot() +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
Also you can just change from geom_boxplot() to geom_boxplot(aes(fill=Type)) in you original codes.
ex:
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot(aes(fill=Type)) +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
What has to change is
geom_boxplot() +
to
geom_boxplot(fill = c("white","white","white","red","blue","white")) +
and remove
scale_fill_manual(values = c("white","white","white","red","blue","white"))

can one offset jitter points in ggplot boxplot

In a ggplot boxplot, it is easy to use jitter to add the raw data points with varying degrees of jitter. With zero jitter the following code
dat <- data.frame(group=c('a', 'b', 'c'), values = runif(90))
ggplot(dat, aes(group, values)) +
geom_boxplot(outlier.size = 0) +
geom_jitter(position=position_jitter(width=0), aes(colour=group), alpha=0.7) +
ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') +
opts(legend.position = "right") + ylab("values") + xlab("group")
produces the plot below.
Is it possible to use zero jitter but add an offset such that the points are in a line but shifted left by 25% of the box width? I tried geom_point with dodge but this generated a jitter.
If we convert group to numeric and then add an offset, you seem to get your desired output. There is probably a more effective / efficient way, but give this a whirl:
ggplot(dat, aes(group, values)) +
geom_boxplot(outlier.size = 0) +
geom_point(aes(x = as.numeric(group) + .25, colour=group), alpha=0.7) +
ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') +
opts(legend.position = "right") + ylab("values") + xlab("group")

Resources