以下是我的数据:
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)
我想将条形码名称更改为Windows
,MacOS
,HTML5
。如何使用ggplot
进行配置? (请注意,我无法更改原始数据)
答案 0 :(得分:1)
只需将新的labels
赋予您的x
变量
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)
由reprex package(v0.3.0)于2019-11-10创建