使用包裹在函数中的ggplot2中的重新排序 [英] Use reorder in ggplot2 wrapped in a function

查看:166
本文介绍了使用包裹在函数中的ggplot2中的重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在构建一个函数来包装一些ggplot2,如下所示:

  df < -  data.frame(变量= c(CHK_ACCOUNT3,AMOUNT,DURATION),Overall_Imp= c(71.44,54.24,34.84 ))

clevelandPlot< - function(dataFrame,xValue,yValue){

ggplot(data = dataFrame,aes_string(x = xValue,y = yValue))+
geom_segment(aes_string(yend = yValue),xend = 0,color =grey50)+
geom_point(size = 3)+
scale_x_continuous(expand = c(0.01,0))+
theme_bw()+
theme(panel.grid.major.y = element_blank())+
xlab(重要性)+
ylab()

}

此功能有效:

  clevelandPlot(df,Overall_Imp,Variable)



但是,当我尝试使用以下代码对值进行重新排序时,它不起作用:

  clevelandPlot<  -  function(dataFrame,xValue,yValue){

ggplot(data = dataFrame,aes_string(x = xValue,y = reorder (yValue,xValue)))+
geom_segment(aes_string(yend = yValue),xend = 0,color =grey50)+
geom_point(size = 3)+
scale_x_continuous(expand = c(0.01,0))+
theme_bw()+
theme(panel.grid.major.y = element_blank())+
xlab(重要性)+
ylab()

}

strong>



如何在y轴上按照递减顺序重新排列变量(图表顶部的最大值)?

解决方案

  clevelandPlot<  -  function(dataFrame,xValue,yValue){

ix< - sort.int(dataFrame [,xValue],递减= FALSE,index.return = TRUE)$ ix
dataFrame [,y Value]< - factor(dataFrame [,yValue],dataFrame [ix,yValue])

ggplot(data = dataFrame,aes_string(x = xValue,y = yValue))+
geom_segment(aes_string(yend = yValue),xend = 0,color =grey50)+
geom_point(size = 3)+
scale_x_continuous(expand = c(0.01,0))+
theme_bw()+
theme(panel.grid.major.y = element_blank())+
xlab(重要性)+
ylab()
}

clevelandPlot(df,Overall_Imp,Variable)


Background

I am building a function to wrap some ggplot2 as follow:

df <- data.frame("Variable" = c("CHK_ACCOUNT3", "AMOUNT", "DURATION"), "Overall_Imp" = c(71.44, 54.24, 34.84))

    clevelandPlot <- function (dataFrame, xValue, yValue) {

  ggplot(data = dataFrame, aes_string(x=xValue, y=yValue)) +
    geom_segment(aes_string(yend = yValue), xend = 0, colour = "grey50") +
    geom_point(size=3) +
    scale_x_continuous(expand = c(0.01, 0)) +
    theme_bw() +
    theme(panel.grid.major.y = element_blank()) +
    xlab("Importance") +
    ylab("")

}

This function works:

clevelandPlot(df, "Overall_Imp", "Variable")

But when I try to reorder the values with the following code, it does not work:

clevelandPlot <- function (dataFrame, xValue, yValue) {

  ggplot(data = dataFrame, aes_string(x=xValue, y=reorder(yValue, xValue))) +
    geom_segment(aes_string(yend = yValue), xend = 0, colour = "grey50") +
    geom_point(size=3) +
    scale_x_continuous(expand = c(0.01, 0)) +
    theme_bw() +
    theme(panel.grid.major.y = element_blank()) +
    xlab("Importance") +
    ylab("")

}

Question

How to reorder the variable in the y axis in a decreasing order (biggest value at the top of the plot)?

解决方案

clevelandPlot <- function (dataFrame, xValue, yValue) {

  ix <- sort.int(dataFrame[,xValue], decreasing = FALSE, index.return = TRUE)$ix
  dataFrame[,yValue] <- factor(dataFrame[,yValue], dataFrame[ix,yValue])

  ggplot(data = dataFrame, aes_string(x=xValue, y=yValue)) +
    geom_segment(aes_string(yend = yValue), xend = 0, colour = "grey50") +
    geom_point(size=3) +
    scale_x_continuous(expand = c(0.01, 0)) +
    theme_bw() +
    theme(panel.grid.major.y = element_blank()) +
    xlab("Importance") +
    ylab("")
}

clevelandPlot(df, "Overall_Imp", "Variable")

这篇关于使用包裹在函数中的ggplot2中的重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆