First I will simulate the data I am working with for replication.
electrode <- c('Cz','Fz','Pz')
value <- c(2.307785,1.613350,2.925053)
sd <- c(0.7593193,0.7612393,0.7568794)
se <- c(0.002829816,0.002836971,0.002820723)
ci <- c(0.005546431,0.005560455,0.005528609)
tgc <- data.frame(electrode,value,sd,se,ci)
col <- c("#004d8d", "#cc2701", "#e5b400")
stdev <- c(10.783939,10.506721,9.415955)
See I also specify the color and then use a manual scale in the ggplot
.
Now if I put the geom_error
before the geom_rect
that adds the shade I want, it works fine but it does layer the shade over the points with error bars, as you can see with the Fz
in the middle.
p1 <- ggplot(tgc, aes(x=factor(electrode), y=value, colour=electrode))
geom_errorbar(aes(ymin=value-sd, ymax=value sd), width=.1, alpha=.5)
geom_rect(inherit.aes = FALSE,mapping =aes(xmin=0, xmax=Inf, ymin=-0.1*min(stdev), ymax=0.1*max(stdev)),fill="grey",alpha = 0.5)
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
labs(title="Difference at max. discrepancy", x="Electrode", y="Signal (µV)")
lims(y = c(-10, 10))
theme_linedraw()
geom_point()
p1
What I am trying to do is change the layer order, and to do so, I interchange places of geom_rect
with geom_errorbar
.
p1 <- ggplot(tgc, aes(x=factor(electrode), y=value, colour=electrode))
geom_rect(inherit.aes = FALSE,mapping =aes(xmin=0, xmax=Inf, ymin=-0.1*min(stdev), ymax=0.1*max(stdev)),fill="grey",alpha = 0.5)
geom_errorbar(aes(ymin=value-sd, ymax=value sd), width=.1, alpha=.5)
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
labs(title="Difference at max. discrepancy", x="Electrode", y="Signal (µV)")
lims(y = c(-10, 10))
theme_linedraw()
geom_point()
p1
And I get the following error message and it does not draw the new plot.
Error: Discrete value supplied to continuous scale
And I don't really understand why this is happening as I am, in fact, specifying fill in geom_rect
.
Any ideas what I am doing wrong? Thanks in advance!
CodePudding user response:
The issue is that by switching the order a continuous x scale is "added" to your plot via the geom_rect
which when adding the geom_errorbar
raises an error as there you are mapping a categorical variable on x
. In contrast, doing it the other way around is fine, i.e. we could map a continuous value on an otherwise discrete scale.
A fix would be to explicitly set a discrete x scale by adding a scale_x_discrete
before the geom_rect
:
library(ggplot2)
ggplot(tgc, aes(x = factor(electrode), y = value, colour = electrode))
scale_x_discrete()
geom_rect(inherit.aes = FALSE, mapping = aes(xmin = 0, xmax = Inf, ymin = -0.1 * min(stdev), ymax = 0.1 * max(stdev)), fill = "grey", alpha = 0.5)
geom_errorbar(aes(ymin = value - sd, ymax = value sd), width = .1, alpha = .5)
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz"))
labs(title = "Difference at max. discrepancy", x = "Electrode", y = "Signal (µV)")
lims(y = c(-10, 10))
theme_linedraw()
geom_point()