征求意见,根据R中的R平方值(从线性回归模型中得出)绘制大变量图(最好使用GGPLOT2)

征求意见,根据R中的R平方值(从线性回归模型中得出)绘制大变量图(最好使用GGPLOT2),r,ggplot2,plot,lattice,R,Ggplot2,Plot,Lattice,我必须为我的项目建立231个线性回归模型。在运行了231个模型之后,我得到了231个R平方值,这些值必须显示在变量名对应的绘图中。由于231个R平方值对于一个表来说太多了,我正在寻找一个绘图方法,这样我就可以将R平方值显示为y轴,变量名显示为x轴。当我运行dput(head(df,5))时,我得到以下信息(这可能会让您了解我的数据): 请注意,我的全部数据有231个观测值,我想将可变带(作为一个因子)绘制为x轴,R平方绘制为y轴。我已经在ggplot2中尝试过geom_point(),但它看起来

我必须为我的项目建立231个线性回归模型。在运行了231个模型之后,我得到了231个R平方值,这些值必须显示在变量名对应的绘图中。由于231个R平方值对于一个表来说太多了,我正在寻找一个绘图方法,这样我就可以将R平方值显示为y轴,变量名显示为x轴。当我运行dput(head(df,5))时,我得到以下信息(这可能会让您了解我的数据):

请注意,我的全部数据有231个观测值,我想将可变带(作为一个因子)绘制为x轴,R平方绘制为y轴。我已经在ggplot2中尝试过geom_point(),但它看起来非常混乱和复杂。有什么想法吗


更新:当我使用@Duck建议的代码时,我得到了这个图,这对于科学演示来说有点混乱。

如果你有大量的值,你可以避开axis中的标签,下面是一个例子:

library(ggplot2)
#Code
ggplot(mdf,aes(x=factor(Band),y=R.squared))+
  geom_point()+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  coord_flip()
输出:

使用的一些数据:

#Data
mdf <- structure(list(Band = c(402, 411, 419, 427, 434, 412, 421, 429, 
437, 444, 422, 431, 439, 447, 454, 432, 441, 449, 457, 464), 
    R.squared = c(0.044655015122032, 0.852028718800355, 0.818617476505653, 
    0.825782272278991, 0.860844967662728, 0.044655015122032, 
    0.852028718800355, 0.818617476505653, 0.825782272278991, 
    0.860844967662728, 0.044655015122032, 0.852028718800355, 
    0.818617476505653, 0.825782272278991, 0.860844967662728, 
    0.044655015122032, 0.852028718800355, 0.818617476505653, 
    0.825782272278991, 0.860844967662728), Adj.Rsquared = c(-0.0614944276421867, 
    0.835587465333728, 0.798463862784058, 0.806424746976656, 
    0.845383297403031, -0.0614944276421867, 0.835587465333728, 
    0.798463862784058, 0.806424746976656, 0.845383297403031, 
    -0.0614944276421867, 0.835587465333728, 0.798463862784058, 
    0.806424746976656, 0.845383297403031, -0.0614944276421867, 
    0.835587465333728, 0.798463862784058, 0.806424746976656, 
    0.845383297403031), Intercept = c(0.000142126282140086, -0.00373545760470339, 
    -0.00258909036368109, 0.000626075834918527, -3.3448513588372e-05, 
    0.000142126282140086, -0.00373545760470339, -0.00258909036368109, 
    0.000626075834918527, -3.3448513588372e-05, 0.000142126282140086, 
    -0.00373545760470339, -0.00258909036368109, 0.000626075834918527, 
    -3.3448513588372e-05, 0.000142126282140086, -0.00373545760470339, 
    -0.00258909036368109, 0.000626075834918527, -3.3448513588372e-05
    ), Slope = c(-0.00108714482110104, 0.393380133190131, 0.443463459485279, 
    0.503881831479685, 0.480162723468755, -0.00108714482110104, 
    0.393380133190131, 0.443463459485279, 0.503881831479685, 
    0.480162723468755, -0.00108714482110104, 0.393380133190131, 
    0.443463459485279, 0.503881831479685, 0.480162723468755, 
    -0.00108714482110104, 0.393380133190131, 0.443463459485279, 
    0.503881831479685, 0.480162723468755)), row.names = c(NA, 
-20L), class = "data.frame")
输出:

另一种选择:

#Code 3
ggplot(mdf,aes(x=reorder(factor(Band), R.squared, mean),y=R.squared))+
  geom_point()+
  geom_segment( aes(x=reorder(factor(Band), R.squared, mean),
                    xend=reorder(factor(Band), R.squared, mean),
                    y=0,
                    yend=R.squared))+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  coord_flip()
输出:


如果有大量值,则可以避开axis中的标签,例如:

library(ggplot2)
#Code
ggplot(mdf,aes(x=factor(Band),y=R.squared))+
  geom_point()+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  coord_flip()
输出:

使用的一些数据:

#Data
mdf <- structure(list(Band = c(402, 411, 419, 427, 434, 412, 421, 429, 
437, 444, 422, 431, 439, 447, 454, 432, 441, 449, 457, 464), 
    R.squared = c(0.044655015122032, 0.852028718800355, 0.818617476505653, 
    0.825782272278991, 0.860844967662728, 0.044655015122032, 
    0.852028718800355, 0.818617476505653, 0.825782272278991, 
    0.860844967662728, 0.044655015122032, 0.852028718800355, 
    0.818617476505653, 0.825782272278991, 0.860844967662728, 
    0.044655015122032, 0.852028718800355, 0.818617476505653, 
    0.825782272278991, 0.860844967662728), Adj.Rsquared = c(-0.0614944276421867, 
    0.835587465333728, 0.798463862784058, 0.806424746976656, 
    0.845383297403031, -0.0614944276421867, 0.835587465333728, 
    0.798463862784058, 0.806424746976656, 0.845383297403031, 
    -0.0614944276421867, 0.835587465333728, 0.798463862784058, 
    0.806424746976656, 0.845383297403031, -0.0614944276421867, 
    0.835587465333728, 0.798463862784058, 0.806424746976656, 
    0.845383297403031), Intercept = c(0.000142126282140086, -0.00373545760470339, 
    -0.00258909036368109, 0.000626075834918527, -3.3448513588372e-05, 
    0.000142126282140086, -0.00373545760470339, -0.00258909036368109, 
    0.000626075834918527, -3.3448513588372e-05, 0.000142126282140086, 
    -0.00373545760470339, -0.00258909036368109, 0.000626075834918527, 
    -3.3448513588372e-05, 0.000142126282140086, -0.00373545760470339, 
    -0.00258909036368109, 0.000626075834918527, -3.3448513588372e-05
    ), Slope = c(-0.00108714482110104, 0.393380133190131, 0.443463459485279, 
    0.503881831479685, 0.480162723468755, -0.00108714482110104, 
    0.393380133190131, 0.443463459485279, 0.503881831479685, 
    0.480162723468755, -0.00108714482110104, 0.393380133190131, 
    0.443463459485279, 0.503881831479685, 0.480162723468755, 
    -0.00108714482110104, 0.393380133190131, 0.443463459485279, 
    0.503881831479685, 0.480162723468755)), row.names = c(NA, 
-20L), class = "data.frame")
输出:

另一种选择:

#Code 3
ggplot(mdf,aes(x=reorder(factor(Band), R.squared, mean),y=R.squared))+
  geom_point()+
  geom_segment( aes(x=reorder(factor(Band), R.squared, mean),
                    xend=reorder(factor(Band), R.squared, mean),
                    y=0,
                    yend=R.squared))+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  coord_flip()
输出:

热图怎么样

dat <- tibble(
  Band = 100:330, 
  R.squared = runif(231, 0, 1), 
  Adj.Rsquared = R.squared - runif(231, 0, .1)
) %>% 
  mutate(Band = reorder(factor(Band), R.squared, mean)) 

q <- c(0, quantile(dat$R.squared, c(.2,.4, .6, .8)), 1)
dat <- dat %>% mutate(group=cut(R.squared, breaks=q))
levels(dat$group) <- c("Quntile 1", "Quintile 2", "Quintile 3", "Quintile 4", "Quintile 5")

ggplot(dat, aes(x=1, y = Band, fill=R.squared)) + 
  geom_tile(col="white") + 
  facet_wrap(~group, scales="free_y", nrow=1) + 
  scale_x_continuous(breaks=NULL) + 
  scale_fill_viridis_c() + 
  labs(x = "", y="", fill=expression(R^2)) + 
  theme(legend.position="top", 
        axis.text.y = element_text(size=10))
dat%
变异(带=重新排序(因子(带),R平方,平均值))
那热图呢

dat <- tibble(
  Band = 100:330, 
  R.squared = runif(231, 0, 1), 
  Adj.Rsquared = R.squared - runif(231, 0, .1)
) %>% 
  mutate(Band = reorder(factor(Band), R.squared, mean)) 

q <- c(0, quantile(dat$R.squared, c(.2,.4, .6, .8)), 1)
dat <- dat %>% mutate(group=cut(R.squared, breaks=q))
levels(dat$group) <- c("Quntile 1", "Quintile 2", "Quintile 3", "Quintile 4", "Quintile 5")

ggplot(dat, aes(x=1, y = Band, fill=R.squared)) + 
  geom_tile(col="white") + 
  facet_wrap(~group, scales="free_y", nrow=1) + 
  scale_x_continuous(breaks=NULL) + 
  scale_fill_viridis_c() + 
  labs(x = "", y="", fill=expression(R^2)) + 
  theme(legend.position="top", 
        axis.text.y = element_text(size=10))
dat%
变异(带=重新排序(因子(带),R平方,平均值))

q我认为@Duck的答案是好的,但是你可以通过按
R.squared
排序
Band
因子来获得更多的里程数,所以你可以使用
reorder(factor(Band),R.squared,mean)
@DaveArmstrong这是一个很好的建议,让我把它包括在你的功劳中吧!谢谢@Duck和DaveArmstrong的建议!我尝试了建议的代码。只有当你有少量的乐队名称时,它看起来才好,但在我的例子中,我有231个乐队名称,当我制作情节时,它看起来非常混乱。我想向您展示我从您提供的代码中获得的情节,但我不确定如何将其添加到此处的评论部分(我是stackoverflow的新用户)。@Zav编辑您的问题,并在其上添加屏幕截图,以便我们可以看到。@Duck我在帖子中编辑并添加了屏幕截图。谢谢你的提示!我认为@Duck的答案是好的,但是你可以通过按
R.squared
排序
Band
因子来获得更多的里程数,因此你可以使用
reorder(factor(Band),R.squared,mean)
@DaveArmstrong这是一个很好的建议,让我把它包括在内,并感谢你!谢谢@Duck和DaveArmstrong的建议!我尝试了建议的代码。只有当你有少量的乐队名称时,它看起来才好,但在我的例子中,我有231个乐队名称,当我制作情节时,它看起来非常混乱。我想向您展示我从您提供的代码中获得的情节,但我不确定如何将其添加到此处的评论部分(我是stackoverflow的新用户)。@Zav编辑您的问题,并在其上添加屏幕截图,以便我们可以看到。@Duck我在帖子中编辑并添加了屏幕截图。谢谢你的提示!谢谢你的构思!虽然这些热图看起来很好,但它们并不能帮助我显示多少详细信息。我想我将不得不使用一个绘图,在这个绘图中,我将标注栏绘制为整数或类似的东西。@Zav也可以使用其他替代方法-您可以想象一个直方图,其中标注栏是由标注栏的名称构建的,或者,根据您的演示方式,包括一个交互式绘图,其中工具提示悬停包含标注栏名称,也可以使用
ggplot()
ggplotly()
进行R平方和调整后的R平方。但是,如果主要目标是让演示文稿中的人员能够清楚地阅读幻灯片上的230个数字及其各自的标签,我不确定是否有符合要求的显示。感谢您的构思!虽然这些热图看起来很好,但它们并不能帮助我显示多少详细信息。我想我将不得不使用一个绘图,在这个绘图中,我将标注栏绘制为整数或类似的东西。@Zav也可以使用其他替代方法-您可以想象一个直方图,其中标注栏是由标注栏的名称构建的,或者,根据您的演示方式,包括一个交互式绘图,其中工具提示悬停包含标注栏名称,也可以使用
ggplot()
ggplotly()
进行R平方和调整后的R平方。但是,如果主要目标是让演示文稿中的人员能够清楚地阅读幻灯片上的230个数字及其各自的标签,我不确定是否有符合要求的显示。