我正在尝试创建一个RMT plot,其中我想要一个双x轴,但两者都显示在底部。我找到了一些使用其他R包的例子(例如this lattice example,但我想知道是否有人知道使用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%,在第二个x轴上为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而不是使用晶格?
答案 0 :(得分:3)
这似乎是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")
的一个很好的用例:
{{1}}