我需要在ggplot上方和下方显示x轴的标签,并与相应的数据点对齐.我已经用必要的数据和美学创建了一个ggplot,但我很难复制x轴,因为它是一个分类变量.

由于scale_x_discrete不可能复制轴,因此我try 使用scale_x_continuous(),用断点拆分类别变量,并将字符载体定义为标签. "plot"是我的数据和美学ggplot. 变量"birds$species_list"是一个包含鸟类物种名称的因子,按百分比值增加排序.

library(ggplot2)
library(data.table)

birds <- data.frame(species_name= c("eagle", "eagle", "robin", "vulture", "bee-eater"),
                 overall.percentage = c(12, 33, 19, 20, 15))

# reorder the species according to their lowest percentage value
x <- setDT(birds)[,.(.(sort(overall.percentage))), species_name]
birds[
  setorder(setDT(transpose(x[[2]]))[,species_name := x[[1]]])[,order := .I],
  on = "species_name", order := order]

plot<-
    ggplot(birds, aes(x=reorder(species_name, order), overall.percentage)) +
    geom_point(position = position_jitter(w = 0.25, h = 0))
    
plot + scale_x_continuous(breaks = 1:length(unique(birds$species_name)),
                     labels = as.character(unique(birds$species_name)),
                     sec.axis = dup_axis())

正如之前网上问题所建议的那样,我也try 了这个代码.

plot + scale_x_continuous(breaks = 1:length(unique(birds$species_name)),
                          labels = as.character(unique(birds$species_name)),
                          sec_axis(~.,
                                   breaks = 1:length(unique(birds$species_name)),
                                   labels = as.character(unique(birds$species_name))))

我原本希望代码能够成功复制我的图中的x轴,但总是向我返回错误"将离散值提供给连续比例". 这可能是与订购操作相关的问题吗?

任何建议都将不胜感激,提前感谢!

推荐答案

其 idea 是,您将离散轴转换为数字轴,自己提供标签,然后您就可以轻松复制轴:

library(ggplot2)

ggplot(iris, aes(x = as.integer(Species), y = Sepal.Width, color = Species)) +
  geom_point(position = position_jitter(height = 0)) + 
  scale_x_continuous("Species",
                     breaks = seq_len(nlevels(iris$Species)),
                     labels = levels(iris$Species),
                     sec.axis = dup_axis()
                     ) +
  theme(panel.grid.minor = element_blank())

Iris Species versus Speal.Width with duplicated axes


根据您的数据,您可以执行以下操作:

library(data.table)

## first modify the data such that species_name is 
## a. a factor
## b. has the factor levels not in alphabetical order but sorted
##    according to overall.percentage
setDT(birds)
birds[, species_name := reorder(factor(species_name), 
                                overall.percentage, min)]

## then do your plot but use the integer positions and add labels manually
ggplot(birds, aes(x = as.integer(species_name), y = overall.percentage)) +
  geom_point(position = position_jitter(w = 0.25, h = 0)) + 
  scale_x_continuous(breaks = birds[, seq_len(nlevels(species_name))],
                     labels = birds[, levels(species_name)],
                     sec.axis = dup_axis()) +
  theme(panel.grid.minor = element_blank())

Scatterplot based on the provided data with 2 axes

R相关问答推荐

在R底座中更改白天和夜晚的背景 colored颜色

如何使用rmarkdown和kableExtra删除包含折叠行的表的第一列的名称

将年度数据插入月度数据

我不能在docker中加载sf

如何优化向量的以下条件赋值?

将文件保存到新文件夹时,切换r设置以不必创建目录

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

在R中使用Scale_y_Break后更改y轴标签

R—将各种CSV数字列转换为日期

用R ggplot2求上、下三角形中两个变量的矩阵热图

按多列统计频次

在多页PDF中以特定布局排列的绘图列表不起作用

在另一个包中设置断点&S R函数

从多层嵌套列表构建Tibble?

从线的交点创建面

有没有办法将基于每个值中出现的两个关键字或短语的字符串向量重新编码为具有这两个值的新向量?

名字的模糊匹配

按组使用dummy r获取高于标准的行的平均值

抽样变换-REXP与RWEIBUR

在不重复主题的情况下重新排列组