Reputation: 1426
Below is my data:
library(ggplot2)
myData <- data.frame(
x = c("windows", "macos", "html5"),
y = c(15, 56, 34)
)
ggplot(myData, aes(x=x, y=y)) +
geom_bar(stat="identity", width = 0.5)
I would like to change the bar names to Windows
, MacOS
, HTML5
. How do I configure that with ggplot
? (Note that I can't change the original data)
Upvotes: 1
Views: 5088
Reputation: 28331
Just give the new labels
to your x
variable
library(tidyverse)
ggplot(myData, aes(x = fct_reorder(x, -y), y = y)) +
geom_col(width = 0.5) +
scale_x_discrete(breaks = c("windows", "macos", "html5"),
labels = c("Windows", "MacOS", "HTML5"))
# or
my_x_labels <- setNames(c("Windows", "MacOS", "HTML5"),
c("windows", "macos", "html5"))
ggplot(myData, aes(x = fct_reorder(x, -y), y = y)) +
geom_col(width = 0.5) +
scale_x_discrete(labels = my_x_labels) +
theme_minimal()
# or
myData <- myData %>%
mutate(x = factor(x,
levels = c("windows", "macos", "html5"),
labels = c("Windows", "MacOS", "HTML5")))
ggplot(myData, aes(x = fct_reorder(x, -y), y = y)) +
geom_col(width = 0.5)
Created on 2019-11-10 by the reprex package (v0.3.0)
Upvotes: 2