复制ggplot的离散x轴 [英] Duplicating discrete x-axis for ggplot
问题描述
这类似于另一篇文章,但是我想在这里做的是在顶部和底部重复x轴,而不仅仅是将其移到顶部.我尝试使用scale_x_discrete(sec.axis = dup_axis())
,但是没有用.以下是我的工作示例
Hi this is similar to another post however what I wanted to do here is duplicate x-axis for top and bottom and not just move it to the top. I tried using scale_x_discrete(sec.axis = dup_axis())
but that did not work. Below is my working example
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显示在顶部和底部.
The plot currently looks like this but I want x to show up top and bottom.
推荐答案
scale_x_discrete
函数没有第二个轴参数,但是scale_x_continuous
有.因此,将type
变量编辑为数字变量,然后更改标签即可:
The scale_x_discrete
function does not have a second axes argument, but scale_x_continuous
does. So editing the type
variable to a numeric variable and then changing the label would work:
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())
这篇关于复制ggplot的离散x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!