簡體   English   中英

底部使用g中的ggplot雙x軸

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

我正在嘗試創建一個RMT圖 ,其中我想要一個雙x軸,但都顯示在底部。 我找到了一些使用其他R包的例子(例如這個格子示例,但我想知道是否有人知道使用ggplot2的方法嗎?

創建類似於我的假數據框,每個數據點由a,b,c的不同百分比組成(注意a + b + c =每個點100%)。 同時我想要三種不同顏色的植被點:

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)

使用兩個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

斜率線顯示因子c為0%的位置(如果因子a和b為0則為100%,因此在原點中)。

我試着用

 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)))

迫使他們都到頂部,但他們只是切換。 有沒有人有關於我應該去哪個方向的提示,或者我應該放棄ggplot並使用晶格代替?

這看起來像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