I have been trying to find a solution to this, but have been unsuccessful as I am not 100% sure what is causing the issue. As this is the last little thing keeping me from finsihing this project, any ideas would be sooo much appreciated!
My ggplot legend looks like this:
As you can see, the linetype and colour are displayed combined, but there seems to be an additional solid line going through the linetypes, making them hard to read. I think this comes from an additional geom_line call to display the grey lines (raw data).
Here is my code minus the theme and labelling
ggplot(table_mean_a, mapping= aes(x = phase_bins, y = Mean)) +
geom_point(aes(x = phase_bins, y = Mean, colour = cond_f))+
geom_line(df_plotting_a,
mapping = aes(x=phase_bins, y = mean_change_to_base, group=interaction(ID, cond_f)),
alpha = 0.3, size =0.5, colour = "grey", inherit.aes = FALSE)+
geom_hline(yintercept=0, colour="#666766", linetype = "dashed")+
geom_errorbar(subset(table_mean_a, phase_bins!= "baseline"),
mapping=aes(ymin=Mean-SD, ymax=Mean+SD, colour = cond_f),
width=.4, size = 0.7)+
geom_line(aes(group=as.numeric(cond_f), colour = cond_f, linetype=cond_f), size = 1.2)+
facet_wrap(sample~., labeller=labeller(sample = labels), ncol=2, scales = "free_x") +
scale_colour_manual(values=apa)+
labs(colour = "Condition:", linetype = "Condition:")+
guides(colour = guide_legend(override.aes = list(shape = NA, alpha = 1), byrow = TRUE))+
scale_x_discrete(labels= c("base", "stim1", "stim2", "recovery", "break"), drop = T, expand = c(0,0.25))+
scale_y_continuous(limits = c(-1,1), breaks = c(-1,-0.5,0,0.5,1),
labels = scales::number_format(accuracy = 1))+
scale_linetype_manual(values=c("solid", "81", "11", "5111"))
Here is the data
table_mean_a <-
structure(list(cond_f = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("artificial", "cry", "laugh", "babble"
), class = "factor"), phase_bins = structure(c(2L, 3L, 4L, 5L,
6L, 2L, 3L, 4L, 5L, 6L), .Label = c("pre", "baseline", "stim_bin1",
"stim_bin2", "recovery", "break"), class = "factor"), age_group = c("adult",
"adult", "adult", "adult", "adult", "adult", "adult", "adult",
"adult", "adult"), sample = structure(c(3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L), .Label = c("UKi", "UGi", "UKa"), class = "factor"),
Mean = c(0, -0.0616666666666667, -0.143333333333333, -0.0983333333333328,
-0.0231666666666662, 0, -0.0949999999999997, -0.1625, -0.113333333333333,
-0.155833333333333), SD = c(0, 0.154096119467973, 0.24172866718288,
0.423777912706934, 0.633257128379362, 0, 0.13934894565432,
0.248908435688157, 0.350921926972951, 0.677239732274149),
Min. = c(0, -0.366666666666667, -0.583333333333337, -0.716666666666667,
-0.98, 0, -0.466666666666665, -0.650000000000003, -0.783333333333337,
-1.96), Max. = c(0, 0.283333333333334, 0.383333333333336,
1.08333333333334, 1.72666666666667, 0, 0.199999999999996,
0.233333333333333, 0.5, 0.846666666666667), count = c(20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L)), row.names = c(NA,
-10L), groups = structure(list(cond_f = structure(c(1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("artificial", "cry",
"laugh", "babble"), class = "factor"), phase_bins = structure(c(2L,
3L, 4L, 5L, 6L, 2L, 3L, 4L, 5L, 6L), .Label = c("pre", "baseline",
"stim_bin1", "stim_bin2", "recovery", "break"), class = "factor"),
age_group = c("adult", "adult", "adult", "adult", "adult",
"adult", "adult", "adult", "adult", "adult"), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
df_plotting_a <-
structure(list(ID = structure(c(69L, 69L, 69L, 69L, 69L), .Label = c("UG201",
"UG208", "UG209", "UG211", "UG215", "UG217", "UG219", "UG220",
"UG221", "UG222", "UG228", "UG243", "UG247", "UG254", "UG268",
"UG271", "UG272", "UG273", "UG274", "UG275", "UG280", "UG283",
"UG284", "UG286", "UG297", "UG299", "UG308", "UG310", "UG315",
"UG316", "UG330", "UG331", "UG334", "UG335", "UK103", "UK104",
"UK105", "UK106", "UK107", "UK108", "UK110", "UK111", "UK112",
"UK113", "UK115", "UK116", "UK117", "UK119", "UK122", "UK123",
"UK130", "UK132", "UK135", "UK136", "UK138", "UK139", "UK140",
"UK142", "UK145", "UK147", "UK150", "UK153", "UK155", "UK156",
"UK159", "UK160", "UK162", "UK164", "UKA102", "UKA103", "UKA104",
"UKA105", "UKA106", "UKA107", "UKA108", "UKA109", "UKA110", "UKA111",
"UKA112", "UKA113", "UKA114", "UKA115", "UKA116", "UKA117", "UKA119",
"UKA120", "UKA121", "UKA122"), class = "factor"), sex = structure(c(2L,
2L, 2L, 2L, 2L), .Label = c("f", "m"), class = "factor"), trial = structure(c(1L,
1L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4"), class = "factor"),
cond_f = structure(c(4L, 4L, 4L, 4L, 4L), .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), age_group = c("adult",
"adult", "adult", "adult", "adult"), stimulus = structure(c(7L,
7L, 7L, 7L, 7L), .Label = c("a1", "a2", "a3", "a4", "b1",
"b2", "b3", "b4", "c1", "c2", "c3", "c4", "l1", "l2", "l3",
"l4"), class = "factor"), phase_bins = structure(2:6, .Label = c("pre",
"baseline", "stim_bin1", "stim_bin2", "recovery", "break"
), class = "factor"), mean_change_to_base = c(0, -0.149999999999997,
-0.399999999999996, -0.299999999999996, -0.0733333333333306
), max_change_to_base = c(0, -0.333333333333329, -0.533333333333331,
-0.43333333333333, -0.333333333333329), sample = structure(c(3L,
3L, 3L, 3L, 3L), .Label = c("UKi", "UGi", "UKa"), class = "factor")), row.names = c(NA,
-5L), groups = structure(list(ID = structure(69L, .Label = c("UG201",
"UG208", "UG209", "UG211", "UG215", "UG217", "UG219", "UG220",
"UG221", "UG222", "UG228", "UG243", "UG247", "UG254", "UG268",
"UG271", "UG272", "UG273", "UG274", "UG275", "UG280", "UG283",
"UG284", "UG286", "UG297", "UG299", "UG308", "UG310", "UG315",
"UG316", "UG330", "UG331", "UG334", "UG335", "UK103", "UK104",
"UK105", "UK106", "UK107", "UK108", "UK110", "UK111", "UK112",
"UK113", "UK115", "UK116", "UK117", "UK119", "UK122", "UK123",
"UK130", "UK132", "UK135", "UK136", "UK138", "UK139", "UK140",
"UK142", "UK145", "UK147", "UK150", "UK153", "UK155", "UK156",
"UK159", "UK160", "UK162", "UK164", "UKA102", "UKA103", "UKA104",
"UKA105", "UKA106", "UKA107", "UKA108", "UKA109", "UKA110", "UKA111",
"UKA112", "UKA113", "UKA114", "UKA115", "UKA116", "UKA117", "UKA119",
"UKA120", "UKA121", "UKA122"), class = "factor"), trial = structure(1L, .Label = c("1",
"2", "3", "4"), class = "factor"), cond_f = structure(4L, .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), .rows = structure(list(
1:5), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, -1L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
The legends from geom_errorbar and geom_line are overlapping together, therefore you see the solid line. You can use show.legend = FALSE in geom_errorbar to hide it.
Since some of your element used in ggplot is missing, here I only include the code for geom_errorbar.
geom_errorbar(subset(table_mean_a, phase_bins!= "baseline"),
mapping=aes(ymin=Mean-SD, ymax=Mean+SD, colour = cond_f),
width=.4, size = 0.7, show.legend = F)
Related
I have problem ploting credibility interval like this:
My data structure is following,L1,L2,M,U1,U2 stand for 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant,respectively.
`
structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")`
I referenced this answerenter link description here,but 'fill' seems only work for boxplot case.the code I tried so far is:
CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)
It is far away from what I want.
Many thanks!
How about the following:
ggplot(df, aes(x = param, y = M, colour = approach)) +
geom_point(position = position_dodge2(width = 0.3), size = 3) +
geom_linerange(
aes(ymin = L2, ymax = U2, x = param),
position = position_dodge2(width = 0.3),
size = 2) +
geom_linerange(
aes(ymin = L1, ymax = U1, x = param),
position = position_dodge2(width = 0.3),
size = 1) +
coord_flip() +
labs(x = "Parameter", y = "Estimate")
Sample data
df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")
This might seem a really stupid mistake on my part but whenever I specify geom_point depending on a factor and choose a hollow point and a solid point (shapes 1 and 19), and plot error bars, it crosses the point.
Here are my data frames:
> dput(head(allbins.sum))
structure(list(T = c(0L, 0L, 10L, 10L, 20L, 20L), treatment = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("control bead", "dP bead"), class = "factor"),
N = c(3, 3, 3, 3, 3, 3), cellsBase = c(0, 0, 0.013028995209506,
0.135599858885737, -0.0130289952095061, 0.759359209760127
), sd = c(0, 0, 0.0597063567767786, 0.0469731690178533, 0.0983667566897066,
0.183436089048999), se = c(0, 0, 0.034471481157405, 0.0271199717771474,
0.0567920734541125, 0.105906875391532), ci = c(0, 0, 0.148318812500416,
0.116687820597672, 0.244356569875469, 0.455680506502609),
bin = c("BinA", "BinA", "BinA", "BinA", "BinA", "BinA")), .Names = c("T",
"treatment", "N", "cellsBase", "sd", "se", "ci", "bin"), row.names = c(NA,
6L), class = "data.frame")
> dput(head(allbins.fitdata))
structure(list(wellvidbin = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("A1-002-BinA", "A1-002-BinB", "A1-002-BinC", "A1-031-BinA",
"A1-031-BinB", "A1-031-BinC", "A3-004-BinA", "A3-004-BinB", "A3-004-BinC",
"B1-032-BinA", "B1-032-BinB", "B1-032-BinC", "B4-026-BinA", "B4-026-BinB",
"B4-026-BinC", "C4-027-BinA", "C4-027-BinB", "C4-027-BinC"), class = "factor"),
treatment = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control bead",
"dP bead"), class = "factor"), wellvid = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("A1-002", "A1-031", "A3-004",
"B1-032", "B4-026", "C4-027"), class = "factor"), bin = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("BinA", "BinB", "BinC"), class = "factor"),
T = c(0L, 10L, 20L, 30L, 40L, 50L), T.factor = structure(1:6, .Label = c("0",
"10", "20", "30", "40", "50", "60"), class = "factor"), cells = c(7L,
11L, 26L, 27L, 28L, 36L), cellsS = c(-1.36568429306349, -1.20296446240061,
-0.592765097414793, -0.552085139749072, -0.511405182083351,
-0.185965520757582), cellsBase = c(0, 0.162719830662884,
0.772919195648701, 0.813599153314422, 0.854279110980143,
1.17971877230591), treatT = structure(c(2L, 4L, 6L, 8L, 10L,
12L), .Label = c("control bead.0", "P bead.0", "control bead.10",
"P bead.10", "control bead.20", "P bead.20", "control bead.30",
"P bead.30", "control bead.40", "P bead.40", "control bead.50",
"P bead.50", "control bead.60", "P bead.60"), class = "factor"),
fit = c(0.0285939715820639, 0.304399288764407, 0.58020460594675,
0.856009923129092, 1.13181524031144, 1.40762055749378), se.fit = c(0.157415367032567,
0.132348142293459, 0.114707848741265, 0.108190467052118,
0.114707848741265, 0.132348142293459), upr = c(0.337128090965895,
0.563801647659587, 0.805031989479629, 1.06806323855124, 1.35664262384431,
1.66702291638896), lwr = c(-0.279940147801767, 0.0449969298692267,
0.35537722241387, 0.643956607706942, 0.906987856778556, 1.1482181985986
)), .Names = c("wellvidbin", "treatment", "wellvid", "bin",
"T", "T.factor", "cells", "cellsS", "cellsBase", "treatT", "fit",
"se.fit", "upr", "lwr"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000000100788>)
And the code:
ggplot(data=allbins.sum, aes(x=T, y=cellsBase, shape=treatment)) + geom_point(size=5, aes(shape=treatment))+
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
geom_smooth(data=allbins.fitdata, size=1, aes(y=fit, ymin=lwr, ymax=upr),
color="black", method="lm", stat="identity", alpha=0.2)+
facet_grid(bin~.) +
scale_shape_manual(values=c(1, 19))
This gives me this plot:
Any hints on how to have the hollow circles to be hollowed?
I also tried specifying geom_shape (aes(fill=treatment) and then scale_fill_manual but then it is also applied to my geom_smooth
Thanks for the help!
If you mean that you don't want the line of the error bar to be visible through the 'hollow' points, then plot geom_errorbar first, then plot geom_point second, with solid fill, so it will overlay the error bar.
ggplot(data=allbins.sum, aes(x=T, y=cellsBase)) +
# plotting this first
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
# plotting this second, with a hollow fillable shape, and black outline
geom_point(size=5, shape = 21, color='black',
aes(fill = treatment)) +
# solid black and solid white fill for the points
scale_fill_manual(values = c('black', 'white')) +
theme_bw()
(The data you posted only has these points for allbins.sum, and the code for allbins.fitdata has an error, so no error bars on this plot)
I tried to make the title self-explanatory, but here goes - data first:
dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"),
ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("srednja škola", "fakultet"), class = "factor"),
`(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139,
33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692,
23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable",
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df",
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma",
"fla"), class = "factor"), ustanova = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola",
"fakultet"), class = "factor")), .Names = c("variable", "ustanova"
), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola",
"vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola",
"fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"),
structure(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))
And I'd like to create a dodged barplot, do the coord_flip and put some text labels inside the bars:
ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +
coord_flip()
you can see output here.
I reckon I'm asking for something trivial. I want the text labels to "follow" stacked bars. Labels are placed correctly on the y-axis, but how to position them correctly on x-axis?
Is this what you want?
library(ggplot2)
ggplot(bar) +
geom_col(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`), group = ustanova),
position = position_dodge(width = .9)) +
coord_flip()
The key is to position = position_dodge(width = .9) (where .9 is the default width of the bars) instead of position = "dodge", which is just a shortcut without any parameter. Additionally you have to set the group=ustanova aesthetic in geom_text to dodge the labels by ustanova (A second option would be to make fill = ustanova a global aesthetic via ggplot(bar, aes(fill = ustanova))
In ggplot2_2.0.0 you find several examples in ?geom_text on how to position geom_text on dodged or stacked bars (the code chunk named "# Aligning labels and bars"). The Q&A What is the width argument in position_dodge? provides a more thorough description of the topic.
I have the following barchart to which I want to add error bars.
library(lattice)
barchart(Change~fTreat,groups=Process,change,
auto.key=list(points=FALSE,rectangles=TRUE),
panel=function(x, y,...){
panel.barchart(x,y,origin = 0,...);
panel.abline(h=0,col="black",...);
}
)
I have tried using the panel.errbars from the memisc package which works great for xyplots, but when I add it to my code it does not respect the groups.
library(memisc)
barchart(cbind(Change,lower,upper)~fTreat,groups=Process,change,
ylab="Pocertage change",
ylim=-115:50,
scales=list(alternating=FALSE,
tick.number=7,
tck=c(-1,0)),
panel=function(x, y,groups,...){
panel.barchart(x,y=change$Change,groups=change$Process,origin = 0,...);
panel.abline(h=0,col="black",...);
panel.errbars(x,y,make.grid="none",ewidth=0.2,type="n",...)
}
)
Any ideas of how to add error bars to my plot either using the panel.errbars or any other function?
The data:
structure(list(Treat = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L), .Label = c("12-380", "12-750", "8-380", "8-750"), class = "factor"),
Process = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Resp",
"Cal"), class = c("ordered", "factor")), Change = c(-33.05,
-34.74, 20.94, 18.06, 6.85, -28.57, -8.1, -78.72), upper = c(-13.22896628,
-28.61149669, 31.29930461, 27.30173776, 39.73271282, 9.458372948,
13.11035572, -47.03745704), lower = c(-52.86120694, -40.87446411,
10.57421563, 8.822042178, -26.03144161, -66.60447035, -29.30563327,
-110.3973761), fTreat = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L), .Label = c("8-380", "8-750", "12-380", "12-750"), class = c("ordered",
"factor"))), .Names = c("Treat", "Process", "Change", "upper",
"lower", "fTreat"), row.names = c(NA, -8L), class = "data.frame")
Cheers
Here is another answer I was given using lattice.
prepanel=function(y, stderr, subscripts=subscripts, ...){
uy <- as.numeric(y+stderr[subscripts])
ly <- as.numeric(y-stderr[subscripts])
list(ylim=range(y,uy,ly, finite=TRUE))
}
panel.err=function(x, y, subscripts, groups, stderr, box.ratio, ...){
d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
panel.arrows(as.numeric(x)+g,y-stderr[subscripts], as.numeric(x)+g, y+stderr[subscripts],
code=3,angle=90, length=0.025)
}
barchart(Change~fTreat,groups=Process,change,
stderr=change$stderr,
ylab="Pocertage change",
xlab="Treatment",
ylim=-115:50,
auto.key=list(points=FALSE,rectangles=TRUE,columns=2),
scales=list(alternating=FALSE,
tick.number=7,
tck=c(-1,0)),
prepanel=prepanel,
panel=function(x, y, subscripts, groups, stderr, box.ratio, ...){
panel.barchart(x, y, subscripts=subscripts,
groups=groups, box.ratio=box.ratio,origin=0, ...)
panel.abline(h=0,col="black",...)
panel.err(x, y, subscripts=subscripts,
groups=groups, box.ratio=box.ratio,stderr=change$stderr)
}
)
A big thank you to Walmes Marques Zeviani for providing the code
Here is the modified data:
change <- structure(list(Treat = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L), .Label = c("12-380", "12-750", "8-380", "8-750"), class = "factor"),
Process = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Respiration",
"Calcification"), class = c("ordered", "factor")), Change = c(-33L,
-35L, 21L, 18L, 7L, -29L, -8L, -79L), stderr = c(20L, 6L,
10L, 9L, 33L, 38L, 21L, 32L), fTreat = structure(c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L), .Label = c("8-380", "8-750", "12-380",
"12-750"), class = c("ordered", "factor"))), .Names = c("Treat",
"Process", "Change", "stderr", "fTreat"), row.names = c(NA, -8L
), class = "data.frame")
This is not what you're asking for, but the plot is rather easy to make with ggplot2 (in a case that this is an option)
dt <- structure(list(Treat = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L), .Label = c("12-380", "12-750", "8-380", "8-750"), class = "factor"),
Process = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Resp",
"Cal"), class = c("ordered", "factor")), Change = c(-33.05,
-34.74, 20.94, 18.06, 6.85, -28.57, -8.1, -78.72), upper = c(-13.22896628,
-28.61149669, 31.29930461, 27.30173776, 39.73271282, 9.458372948,
13.11035572, -47.03745704), lower = c(-52.86120694, -40.87446411,
10.57421563, 8.822042178, -26.03144161, -66.60447035, -29.30563327,
-110.3973761), fTreat = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L), .Label = c("8-380", "8-750", "12-380", "12-750"), class = c("ordered",
"factor"))), .Names = c("Treat", "Process", "Change", "upper",
"lower", "fTreat"), row.names = c(NA, -8L), class = "data.frame")
a <- ggplot(dt, aes(y = Change, x = Treat, ymax = upper, ymin = lower))
dodge <- position_dodge(width=0.9)
a + geom_bar(aes(fill = Process), position = dodge) +
geom_errorbar(aes(fill = Process), position = dodge, width = 0.2)
I tried to make the title self-explanatory, but here goes - data first:
dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"),
ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("srednja škola", "fakultet"), class = "factor"),
`(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139,
33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692,
23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable",
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df",
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma",
"fla"), class = "factor"), ustanova = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola",
"fakultet"), class = "factor")), .Names = c("variable", "ustanova"
), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola",
"vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola",
"fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"),
structure(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))
And I'd like to create a dodged barplot, do the coord_flip and put some text labels inside the bars:
ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +
coord_flip()
you can see output here.
I reckon I'm asking for something trivial. I want the text labels to "follow" stacked bars. Labels are placed correctly on the y-axis, but how to position them correctly on x-axis?
Is this what you want?
library(ggplot2)
ggplot(bar) +
geom_col(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`), group = ustanova),
position = position_dodge(width = .9)) +
coord_flip()
The key is to position = position_dodge(width = .9) (where .9 is the default width of the bars) instead of position = "dodge", which is just a shortcut without any parameter. Additionally you have to set the group=ustanova aesthetic in geom_text to dodge the labels by ustanova (A second option would be to make fill = ustanova a global aesthetic via ggplot(bar, aes(fill = ustanova))
In ggplot2_2.0.0 you find several examples in ?geom_text on how to position geom_text on dodged or stacked bars (the code chunk named "# Aligning labels and bars"). The Q&A What is the width argument in position_dodge? provides a more thorough description of the topic.