Consider this plot
dat <- data.frame(Sample = c(rep("S01", 3),
rep("S02", 3),
rep("S03", 3)),
Class = as.factor(c(rep(c("A", "B", "C"),3))),
Abundance=c(0.3, 0.4, 0.3, 0.3, 0.3, 0.4, 0.4, 0.3, 0.3))
dat <- rbind(dat, dat, dat)
dat$Transect <- as.factor(c(rep("x", 9), rep("y", 9), rep("z", 9)))
dat <- rbind(dat, dat, dat, dat)
dat$Season <- as.factor(c(rep("Ap", 27), rep("Ma", 27), rep("Au", 27), rep("Se", 27)))
ggplot(dat, aes(Sample, Abundance, fill=Class)) +
theme_bw() +
geom_bar(col="black", stat="identity", width=0.75) +
facet_grid(Season~Transect)
it is not possible to relabel the x-axis with the labels argument within scale_x_discrete
by names given in an additional column, so that S01 is replaced with 100, S02 is replaced with 200, and so on:
dat$Distance <- revalue(dat$Sample, c("S01" = "100",
"S02" = "200",
"S03" = "300"))
ggplot(dat, aes(Sample, Abundance, fill=Class)) +
theme_bw() +
geom_bar(col="black", stat="identity", width=0.75) +
facet_grid(Season~Transect)+
scale_x_discrete(labels = dat$Distance) # note that labels=Distance is not working
All axis labels are set to the first level of the naming factor.
Note, that in my much more complex real life data, it is not possible to switch to aes(x=Distance)
, because of the presence of 4 replicates per Sample, which for unknown reasons prevents the plotting of the bars. Also, putting labels = Distance
into aes
is not working. To the same end, dat$Sample
cannot be made a factor. All these approaches would not result in the desired plot.
Is it possible to have my axis labels replaced?