0

I have searched for quite some while but could not find a conclusive answer, although this is quite a basic problem.

I use geom_bar (ggplot2) to visualize my data. The names of the x-axis groups are directly inferred from the input dataframe. However, I would like to change the names without changing the respective dataframe column. I want to do this because I would like to do some basic text editing of the names (using text subscripts).

I tried scale_x_discrete but had no success. Somehow, I could not specify the limits and make the data visible at the same time.

In the following I have a minimal example. Let's say I would like to change the letters A-E to B-F without touching the actual dataframe.

Master = data.frame(x = rep(LETTERS[1:5], 3), y = runif(15, 0, 1), fac = c(rep(1,5), rep(2,5), rep(3,5)))
Master[,3] = as.factor(Master[,3])

## plotting
p = ggplot(Master, aes(fill=fac, y=y, x=x)) + 
  geom_bar(position="dodge", stat="identity") +
  scale_y_continuous(breaks = c(0,0.2,0.4,0.6,0.8,1), labels = c(0,0.2,0.4,0.6,0.8,1), limits = c(0,1)) + 
  labs(title="", x="", y="") +
  theme_light() +
  theme(axis.title.y = element_text(size = rel(2), angle = 90)) +
  theme(axis.title.x = element_text(size = rel(2), angle = 0)) +
  theme(axis.text.x = element_text(color="black", size=16, angle=0)) +
  theme(axis.text.y = element_text(color="black", size=16, angle=0)) +
  scale_fill_manual(values=c("coral", "#C77CFF", "red", "cyan", "orange", "darkgrey"), 
                    name="fac",
                    breaks=seq(1,6,1),
                    labels=seq(1,6,1)) + 
  theme(legend.title = element_text(colour="black", size=18, face="bold")) + 
  theme(legend.text = element_text(colour="black", size = 16)) +
  theme(legend.key.size=unit(0.8,"cm")) 
print(p)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Arne
  • 337
  • 1
  • 6
  • 17

0 Answers0