Bar chart with error bar using log10 trans - r

I'm new using R and ggplot library, and I'm trying to build a bar chart with an error bar, like this:
pd <- position_dodge(0.80)
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1)
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)")
The only thing is that I would like logscale the y-axis, so I tried using the scale_y_log10 function:
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1)
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)")
+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x)))
But I have a bizarre result, where the errorbar has another scale, different from the bar chart.
How can I fix this? I tried using + ylim(10^-2, 10^1) but it doesn't work.

I just want to quote #eipi10
A bar chart doesn't work well on a log scale, because the bars' baseline is set to 1 (10^0) instead of zero
This explains, why the images go wrong.

Related

How to show the part of the errorbar lines which are within the plot margins using `ggplot2`?

I have a grid of plots, all with the same y and x-axis scale. The plots represent time in the x-axe and mean values in the y-axe with their standard errors. My problem is that some errorbars are not entirely within the plot margins, and I wonder if there is some way to represent the part of the errorlines that are within the plot margins. Below I give a fake example and code to play with:
df <- data.frame(time=seq(-15,15,1),
mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))
Plot <- ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))
Plot
You need to set coord_cartesian limits rather than scale_y_continuous limits:
ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
coord_cartesian(ylim = c(0.49, 0.85))

Center the bars in Funnel chart

I'm trying to make funnel chart in R. As an example I took code from this page https://analyzecore.com/2015/06/23/sales-funnel-visualization-with-r/
But the strange thing is, which I could not get, why my bars are skewed to the left side?
Funnel data:
Category Number
1 total_members 1000
2 paid_members 936
3 cancellations 452
4 refunds 34
library(ggplot2)
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols)+
geom_bar(data=funnel_data, aes(x=Category, y=Number, fill=Category), stat="identity", width=1)
Results in
If you just run the piece of code from the article, this one for example:
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), stat="identity", width=1)
It will give you a nice example with bars centered by X:
I have no clue what is the issue in this case. Would be very glad for any assisance.
This is different approach, but works - create second geom_bar geom with negative values:
library(ggplot2)
# Using OPs data
ggplot(funnel_data, aes(Category, fill = Category)) +
geom_bar(aes(y = Number), stat = "identity", width = 1) +
geom_bar(aes(y = -Number), stat = "identity", width = 1) +
theme_minimal() +
coord_flip()

ggplot line color "black" becomes red

I used the following script to make the graph below
hypttauplot <- qplot(fuclin_csf, fitHYPTTAU, data=selTAU, geom=c("smooth"),
method="glm", color='black', linetype=BL_HYPT) +
theme_classic() + xlab("Time (years)") + ylab("Tau (pg/ml)") +
scale_x_continuous(expand=c(0,0)) +
ggtitle("A. Hypertension") +
theme(legend.position = "none")
But now my questions is: why are the lines red instead of black? And how can I change them to black?
You have to use I() to set the aesthetics manually in qplot(), e.g. colour=I("black") (setting vs. mapping of aesthetics).
# mapping
qplot(carat, price, data=diamonds, color="black")
# equivalent to ggplot(data=diamonds, aes(carat, price, color="black")) + geom_point()
# setting
qplot(carat, price, data=diamonds, color=I("black"))
# equivalent to ggplot(data=diamonds, aes(carat, price), color="black") + geom_point()
You can use scale_colour_manual to specify an own set of mappings.

R - Background colour on ggplot split across a diagonal?

Using R, library(ggplot2)
I have a ggplot that is a perfect square. From the top left corner, to the bottom right corner, I have a line.
So say xlim(0,100), ylim(0,100) with geom_abline(intercept=100, slope=-1).
How do I make everything above the line have say a blue background and below a red?
library(ggplot2)
dat = data.frame(x=c(0,100), ymin=0, y=c(100,0), ymax=100)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_abline(aes(intercept=100, slope=-1)) +
#geom_line(aes(x,y), lwd=1) + # If you just want a line with the same extent as the data
theme_bw()
And analogously for more general curves:
x=seq(0,100,0.1)
dat = data.frame(x=x, ymin=0, y=2*x + 3*x^2 - 0.025*x^3)
dat$ymax=max(dat$y)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_line(aes(x,y), colour="yellow", lwd=1) +
theme_bw()

Horizontal lines in errorbars disappearing

I'm having some troubles with errorbars in ggplot (R) similar to this problem in Python.
My horizontal errobars are disappearing when using the scale_y function. Can you help me find a solution? The data is here.
My code is:
data_sac_ggplot <- ggplot(yeast_sac, aes(x=factor(day), y=mean_count, colour=yeastsample, group=c("low","high"))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
geom_line(size=0.8) +
geom_point(size=2, shape=21, fill="white") +
theme_bw()
data_sac_ggplot + geom_errorbar(aes(ymin=mean_count-low, ymax=mean_count+high), width=0.1, position=pd)
data_sac_ggplot + scale_y_log10(breaks=c(1000,10000,100000,1000000,10000000,100000000,1000000000))
Thanks!
Picture of plot:

Resources