gpt4 book ai didi

r - 复制 ggplot 的离散 x 轴

转载 作者:行者123 更新时间:2023-12-04 10:08:21 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Duplicating (and modifying) discrete axis in ggplot2

(1 个回答)


3年前关闭。




嗨,这类似于另一篇文章,但是我想在这里做的是顶部和底部的重复 x 轴,而不仅仅是将其移动到顶部。我尝试使用 scale_x_discrete(sec.axis = dup_axis())但这没有用。下面是我的工作示例

d<- data.frame (pid=c("d","b","c"), type=c("rna","rna","rna"), value = c(1,2,3) )
d2 <- data.frame (pid=c("d","b","c"), type=c("dna","dna","dna"), value = c(10,20,30) )
df <- rbind (d,d2)

ggplot(df, aes(y=pid, x=type ) ) +
geom_tile(aes(fill = value),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
scale_x_discrete(position = "top")
# this failed: scale_x_discrete(sec.axis = dup_axis())

该图目前看起来像这样,但我希望 x 显示在顶部和底部。
enter image description here

最佳答案

scale_x_discrete函数没有第二个轴参数,但 scale_x_continuous做。所以编辑type变量为数字变量,然后更改标签将起作用:

d<- data.frame (pid=c("d","b","c"), type=1, value = c(1,2,3) )
d2 <- data.frame (pid=c("d","b","c"), type= 2, value = c(10,20,30) )
df <- rbind (d,d2)

ggplot(df, aes(y=pid, x=type)) +
geom_tile(aes(fill = value),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
scale_x_continuous(breaks = 1:2,
labels = c("rna", "dna"),
sec.axis = dup_axis())

enter image description here

关于r - 复制 ggplot 的离散 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com