简体   繁体   English

底部使用g中的ggplot双x轴

[英]Double x-axes at bottom using ggplot in R

I am trying to create a RMT plot in which I want a double x-axes, yet both displayed at the bottom. 我正在尝试创建一个RMT图 ,其中我想要一个双x轴,但都显示在底部。 I have found some examples using other R packages (like for example this lattice example but I was wondering if anyone knows a way to do it using ggplot2? 我找到了一些使用其他R包的例子(例如这个格子示例,但我想知道是否有人知道使用ggplot2的方法吗?

Creating fake data frame similar to mine, each data point consists of different percentages of a,b,c (note a+b+c=100% for each point). 创建类似于我的假数据框,每个数据点由a,b,c的不同百分比组成(注意a + b + c =每个点100%)。 At the same time there are three types of vegetation points I want coloured differently: 同时我想要三种不同颜色的植被点:

a <- c(10,30,40,20,30,50,20,10,30,20,60,20)
b <- c(20,30,10,40,60,20,30,60,30,10,20,70)
c <- c(70,40,50,40,10,30,50,30,40,70,20,10)
d <- rep(c("tree", "grass", "herbs"),4)

df_trio <- data.frame(a,b,c,d)

Creating plot with two x-axes (one top and one bottom) with the percentages going from 0-100% on the first x-axis and 100-0% on the second: 使用两个x轴(一个顶部和一个底部)创建绘图,百分比从第一个x轴上的0-100%到第二个轴上的100-0%:

plot_trio <- ggplot(df_trio, aes(a,b)) +
  geom_point(aes(colour = factor(d)))+
  theme_classic()+
  coord_cartesian(xlim = c(0,100), ylim = c(0,100), expand = FALSE)+
  scale_x_continuous(breaks = c(0,10,20,30,40,50,60,70,80,90,100),"%a", 
                 sec.axis = sec_axis((~(.-100)*-1), name = "%c", 
                 breaks = c(100,90,80,70,60,50,40,30,20,10,0)))+
  scale_y_continuous(breaks = c(0,10,20,30,40,50,60,70,80,90,100))+
  ylab("%b")+
  geom_abline(slope = -1, intercept = 100)+
  annotate("text", label="0% of c",x=55,y=52, size=3)

plot_trio

The slope line shows where factor c is 0% (it is 100% if factor a and b are 0, so in the origin). 斜率线显示因子c为0%的位置(如果因子a和b为0则为100%,因此在原点中)。

I tried to use 我试着用

 scale_x_continuous(breaks=c(0,10,20,30,40,50,60,70,80,90,100),"%a", 
                 position="top", sec.axis = sec_axis((~(.-100)*-1), name = 
                 "%c", breaks=c(100,90,80,70,60,50,40,30,20,10,0)))

to force them both to the top but then they just switch. 迫使他们都到顶部,但他们只是切换。 Does anyone have a tip on in which direction I should go to do this, or should I just give up on ggplot and use lattice instead? 有没有人有关于我应该去哪个方向的提示,或者我应该放弃ggplot并使用晶格代替?

This looks like a good use case for ggtern : 这看起来像ggtern一个很好的用例:

df_trio <- data.frame(a = c(10,30,40,20,30,50,20,10,30,20,60,20),
                      b = c(20,30,10,40,60,20,30,60,30,10,20,70),
                      c = c(70,40,50,40,10,30,50,30,40,70,20,10),
                      d = rep(c("tree", "grass", "herbs"),4))
require(ggtern)
ggtern(df_trio, aes(a, b, c, colour = d)) + 
  geom_point(size = 4) + 
  theme_light() + theme_showarrows() + 
  labs(xarrow = "% a", yarrow = "% b", zarrow = "% c")

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM