【发布时间】:2021-11-27 01:07:50
【问题描述】:
关于同一主题还有其他几篇文章,但提出的解决方案不适合我的情况。 给定这样的数据框
ddff <- structure(
list(
SampleID = structure(
20:16,
.Label = c(
"S39",
"S30",
"S35",
"S22",
"S23",
"S26",
"S29",
"S24",
"S27",
"S32",
"S37",
"S36",
"S38",
"S34",
"S33",
"S40",
"S25",
"S28",
"S31",
"S21"
),
class = "factor"
),
Counts = c(12177, 14367, 15118, 15312,
16622),
sampleName = structure(
20:16,
.Label = c(
"2Dr",
"2Es",
"1Er",
"1Bs",
"1Cs",
"2As",
"2Ds",
"1Ds",
"2Bs",
"1Br",
"2Br",
"2Ar",
"2Cr",
"1Dr",
"1Cr",
"2Er",
"1Es",
"2Cs",
"1Ar",
"1As"
),
class = "factor"
),
compartment = c("soil", "root", "soil",
"soil", "root")
),
row.names = c(NA, 5L),
class = "data.frame"
)
还有下面的代码
library(tidyverse)
ddff %>%
ggplot(aes(x = Counts, y = SampleID)) +
geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
geom_text(
aes(label = paste0(" ", Counts)),
size = 3,
hjust = 0,
nudge_x = -0.1,
check_overlap = TRUE,
color = "blue"
) +
xlim(NA, 33000) +
theme_light() +
theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1) +
theme(# remove the vertical grid lines
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()) +
labs(title = "Library Size Overview",
x = "Read Counts",
color = "Compartment") +
theme(
legend.position = c(.95, .95),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.box.background = element_rect(color = "black", size = 1)
)
我明白了这个情节 Rplot 根据 ddff 列“sampleName”的内容,我有兴趣在右侧 Y 轴上添加第二个标签。当然,解决方案似乎是 scale_y_discrete 和 dup_axis 的使用,但我无法弄清楚...根据最近的 ggplot 演变有什么想法?
【问题讨论】:
-
不是
dup_axis,更是如此sec_axis,这略有不同......但我不认为sec_axis本身可以轻松地处理非连续数据,你需要伪造它.有关某种方法,请参阅stackoverflow.com/q/45361904/3358272。