I've made a bar plot with a log scale y-axis using ggplot and stat_summary. When I go to add the mean value using stat_summary's text option, it calculates the log of the mean. When I convert with with exp() it turns all the 0 values into 1. Without calculating the mean in a separate table, is there a way to get stat_summary to insert the true mean?
Here's a small sample of the data:
data <- structure(list(grp = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 2L, 2L, 5L, 5L, 5L, 5L, 2L, 2L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("CSF",
"PC", "NC", "GPC", "standard", "GNC", "QNC"), class = "factor"),
conc = c(`12` = 1.19059894121078, `24` = 0.454058692317884,
`36` = 0.0413144216376913, `48` = 0.0507049983112112, `59` = 39.1536357996621,
`60` = 0.00441419111181165, `71` = 41.4765704973005, `72` = 0.00496996589355793,
`83` = 3.76511709296646, `95` = 6.42853693807243, `7` = 0,
`8` = 0, `12` = 0.493144754137073, `24` = 0.502322800043954,
`36` = 0.0435760888522108, `48` = 0.0490446109926358, `55` = 1.06095474567196,
`56` = 1.09402053731166, `59` = 49.2350172116792, `60` = 0.00521072932352904,
`71` = 52.0902986576751, `72` = 0.00541517396336663, `83` = 4.81558360939545,
`95` = 5.29312800593463, `12` = 0.769326373641471, `24` = 0.619179395297563,
`36` = 0.38693332536866, `48` = 0.398836652989904, `59` = 0.312909059784019,
`60` = 0.375482647786516, `71` = 0.281116313862495, `72` = 0.366649808288524,
`83` = 0.719520752952596, `95` = 1.52458935147364), mda_label = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("none", "mda_20"), class = "factor")), row.names = c(NA,
-34L), class = c("tbl_df", "tbl", "data.frame"))
Plotting with stat_summary geom="text" produces the log(mean):
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label=round(..y..,2)), fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))
I can convert this with exp(), but that also changes all the 0 values to 1.
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label=round(exp(..y..),2)), fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Is there a way around??
CodePudding user response:
You can use an ifelse
:
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label = ifelse(..y.. == 0, 0, round(exp(..y..),2))),
fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))