使用 ggplot 按分面或颜色编码因素绘制同一图形上大量因素的百分比变化
Plotting percent change for a large number of factors on same figure using ggplot by faceting or color-coding factors
这是我正在使用的代码示例
x<-as.factor(rep(c("tree_mean","tree_qmean","tree_skew"),3))
factor<-c(rep("mfn2_burned_99",3),rep("mfna_burned_5_7",3),rep("mfna_burned_5_7_10_12",3)))
y<-c(0.336457409,-0.347422910,-0.318945621,1.494109367, 0.003578698,-0.019985780,-0.484171146, 0.611589217,-0.322292664)
dat<-as.data.frame(cbind(x,factor,y))
head(dat)
x factor y
tree_mean mfn2_burned_99 -0.3364574
tree_qmean mfn2_burned_99 -0.3474229
tree_skew mfn2_burned_99 -0.3189456
tree_mean mfna_burned_5_7 -0.8269814
tree_qmean mfna_burned_5_7 -0.8088810
tree_skew mfna_burned_5_7 -2.5429226
tree_mean mfna_burned_5_7_10_12 -0.8601206
tree_qmean mfna_burned_5_7_10_12 -0.8474920
tree_skew mfna_burned_5_7_10_12 -2.9854178
我正在尝试绘制 x 偏离 0 的程度,并按每个因素对它进行分面,如下所示:
ggplot(dat) +
geom_point(aes(x=x,y=y),shape=1,size=3)+
geom_linerange(aes(x=x,ymin=0,ymax=y))+
geom_hline(yintercept=0)+
facet_grid(factor~.)
当我有三个因素时,这很好用(忽略 *:我有一个显着性列,我已经删除了。
示例如下:
但是,我总共有 8 个因素,分面模糊了情节,使得每个 x 值与零的距离变得非常扭曲。
下面的例子
所以,我的问题是:考虑到我使用分面 或 颜色编码的大量 x 值和因子,coding/rendering 这个图的更好方法是什么按 ggplot 中的因素??
我非常愿意按因子而不是分面对 x 的每个距离进行颜色编码,但我一直在用头撞墙试图弄清楚如何在 ggplot 中做到这一点(对 ggplot 来说非常新) ), 所以我还不能说这是否会使图形更易于解释。
您注意到的一个选项是用一个因子为您的点 and/or 线范围着色。然后,您可以使用 position_dodge
在 x 轴上稍微移动这些点。
例如:
ggplot(dat, aes(color = factor)) +
geom_point(aes(x=x,y=y),shape=1,size=3, position = position_dodge(width = 0.5)+
geom_linerange(aes(x=x,ymin=0,ymax=y), position = position_dodge(width =0.5))+
geom_hline(yintercept=0)
我认为这在很多因素下仍然很困难,但如果有 8 个,它可能适合您的目的。
这是我正在使用的代码示例
x<-as.factor(rep(c("tree_mean","tree_qmean","tree_skew"),3))
factor<-c(rep("mfn2_burned_99",3),rep("mfna_burned_5_7",3),rep("mfna_burned_5_7_10_12",3)))
y<-c(0.336457409,-0.347422910,-0.318945621,1.494109367, 0.003578698,-0.019985780,-0.484171146, 0.611589217,-0.322292664)
dat<-as.data.frame(cbind(x,factor,y))
head(dat)
x factor y
tree_mean mfn2_burned_99 -0.3364574
tree_qmean mfn2_burned_99 -0.3474229
tree_skew mfn2_burned_99 -0.3189456
tree_mean mfna_burned_5_7 -0.8269814
tree_qmean mfna_burned_5_7 -0.8088810
tree_skew mfna_burned_5_7 -2.5429226
tree_mean mfna_burned_5_7_10_12 -0.8601206
tree_qmean mfna_burned_5_7_10_12 -0.8474920
tree_skew mfna_burned_5_7_10_12 -2.9854178
我正在尝试绘制 x 偏离 0 的程度,并按每个因素对它进行分面,如下所示:
ggplot(dat) +
geom_point(aes(x=x,y=y),shape=1,size=3)+
geom_linerange(aes(x=x,ymin=0,ymax=y))+
geom_hline(yintercept=0)+
facet_grid(factor~.)
当我有三个因素时,这很好用(忽略 *:我有一个显着性列,我已经删除了。
示例如下:
但是,我总共有 8 个因素,分面模糊了情节,使得每个 x 值与零的距离变得非常扭曲。
下面的例子
所以,我的问题是:考虑到我使用分面 或 颜色编码的大量 x 值和因子,coding/rendering 这个图的更好方法是什么按 ggplot 中的因素??
我非常愿意按因子而不是分面对 x 的每个距离进行颜色编码,但我一直在用头撞墙试图弄清楚如何在 ggplot 中做到这一点(对 ggplot 来说非常新) ), 所以我还不能说这是否会使图形更易于解释。
您注意到的一个选项是用一个因子为您的点 and/or 线范围着色。然后,您可以使用 position_dodge
在 x 轴上稍微移动这些点。
例如:
ggplot(dat, aes(color = factor)) +
geom_point(aes(x=x,y=y),shape=1,size=3, position = position_dodge(width = 0.5)+
geom_linerange(aes(x=x,ymin=0,ymax=y), position = position_dodge(width =0.5))+
geom_hline(yintercept=0)
我认为这在很多因素下仍然很困难,但如果有 8 个,它可能适合您的目的。