R 有没有办法选择ggplot中x轴上的值标签而不更改数据?

R 有没有办法选择ggplot中x轴上的值标签而不更改数据?,r,ggplot2,R,Ggplot2,数据帧: Techs <- data.frame( Patent_number = c(403, 410, 500, 600, 601, 770), techcategory = c(16, 18, rep(16, 4)) ) # Techs # # Patent_number techcategory # 1 403 16 # 2 410 18 # 3 500

数据帧:

Techs <-
  data.frame(
    Patent_number = c(403, 410, 500, 600, 601, 770),
    techcategory = c(16, 18, rep(16, 4))
  )

# Techs
#
#   Patent_number techcategory
# 1           403           16
# 2           410           18
# 3           500           16
# 4           600           16
# 5           601           16
# 6           770           16

我正在尝试根据数据中的techcategory列创建条形图。但是,该列中使用的标签是数字值,如16和18。我想用文字来代替ggplot中的这些数字。我也不希望他们被重组。无论我用什么词替换“1”,都应该是左边的第一列,以此类推。

你可以用你要找的名字创建一个单独的列

Techs$words <- ifelse(Techs$techcategory == 16, "1", "2") 

通常的方法是使用标签的命名向量。我发现在您的数据中,
techcategory
是一个具有35个级别的因子-因此您应该创建一个静态向量,而不是像我所做的那样在
scale\uu
函数中创建它。然后您只需在磅秤中使用
标签=

ggplot(data=Techs, aes(techcategory, fill = techcategory)) +
  geom_bar() +
  scale_x_discrete("techcategory",
                   labels = c(
                     "18" = "Company1",
                     "16" = "Company2"
                   )) +
  scale_fill_discrete("techcategory",
                   labels = c(
                     "18" = "Company1",
                     "16" = "Company2"
                   ))
ggplot(data=Techs, aes(Techs$words, fill = factor(words))) + geom_bar() +
   ylab("Number of patents") +
   xlab("Category") +
   ggtitle("Random title") +
   theme(plot.title = element_text(hjust = 0.5)) +
   guides(fill=FALSE) 
ggplot(data=Techs, aes(techcategory, fill = techcategory)) +
  geom_bar() +
  scale_x_discrete("techcategory",
                   labels = c(
                     "18" = "Company1",
                     "16" = "Company2"
                   )) +
  scale_fill_discrete("techcategory",
                   labels = c(
                     "18" = "Company1",
                     "16" = "Company2"
                   ))