【问题标题】:Write function for plot in R (ggplot2) [duplicate]在R(ggplot2)中为绘图编写函数[重复]
【发布时间】:2020-06-26 07:13:45
【问题描述】:

我写了一个代码来创建一个情节

#Create a plot mean Calories in each Category
p<-mean_table %>%
  ggplot(aes(mean.Calories,reorder(Category, mean.Calories))) + 
  geom_col(aes(fill = mean.Calories)) + 
  scale_fill_gradient2(low = "forestgreen",
                       high = 'firebrick1',
                       mid = "gold", midpoint = 283.8947)+
  scale_x_continuous(breaks = seq(0, 600, length.out = 7),
                     limits = c(0, 600),
                     labels = seq(0, 600, length.out = 7))+
  xlab("Calories")+
  ylab("Category")+
  ggtitle("Mean of Calories in each Category")+
  theme_minimal()
#Theme for plots
theme<-theme(axis.title.x=element_text(size=16),
        axis.title.y = element_text(size = 16),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black"),
        panel.background = element_rect(color = "black"),
        plot.title = element_text(size = 20,hjust = 0.5),
        legend.position = "none")
#Calories+theme
p+theme 

效果很好。我们可以看到这个情节

但我想写一个函数,因为我需要几个类似的图表。 这是我的变种:

p3<-function(data, x_data, breaks, xlab, title){
  my_plot<-data %>% 
    ggplot(aes(x_data,reorder(Category, x_data))) + 
    geom_col(aes(fill = x_data)) + 
    scale_fill_gradient2(low = "forestgreen",
                       high = 'firebrick1',
                       mid = "gold", midpoint = median(data$x_data))+
    scale_x_continuous(breaks = round(seq(0, breaks, length.out = 7)),
                     limits = c(0,  breaks),
                     labels = round(seq(0,  breaks, length.out = 7)))+
    xlab(xlab)+
    ylab("Category")+
    ggtitle(title)+
    theme_minimal()
  return(my_plot)
}
p3(mean_table, mean_table$mean.Cholesterol, 155, "Gram", "Mean of Fat in each Cholesterol")

我这样使用data.frame(它是小版本):

mean_table<-data.frame(Category=c("Beef & Pork","Beverages","Breakfast"), mean.Cholesterol=c(87.3333333, 0.5555556,     152.8571429))

错误:元素必须等于行数或 1 运行rlang::last_error() 看看哪里出错了。

1:未知或未初始化的列:x_data

我的功能有什么问题?

【问题讨论】:

    标签: r function ggplot2


    【解决方案1】:

    在将列名传递给函数时,您需要使用非标准评估 (NSE)。

    您可以通过两种方式传递列名,即不带引号的列名和带引号的列名。在这个答案中,我们传递不带引号的列名。我们使用 curly-curly ({{}}) 运算符。

    library(ggplot2)
    library(rlang)
    
    p3<-function(data, x_data, breaks, xlab, title){
        my_plot<-data %>% 
                 ggplot(aes({{x_data}},reorder(Category, {{x_data}}))) + 
                 geom_col(aes(fill = {{x_data}})) + 
                 scale_fill_gradient2(low = "forestgreen",
                                     high = 'firebrick1',
                                     mid = "gold", 
                                      midpoint = median(data %>% pull({{x_data}}))) +
                 scale_x_continuous(breaks = round(seq(0, breaks, length.out = 7)),
                                    limits = c(0,  breaks),
                                    labels = round(seq(0,  breaks, length.out = 7)))+
                 xlab(xlab)+
                 ylab("Category")+
                 ggtitle(title) +
                 theme_minimal()
    
       return(my_plot)
    }
    

    并将p3 称为:

    p3(mean_table, mean.Cholesterol, 155, "Gram", "Mean of Fat in each Cholesterol")
    

    如果我们想将列名作为带引号的变量作为“mean.Cholesterol”传递,我们可以使用sym!! 对其进行评估。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2016-12-04
      • 2012-07-05
      • 1970-01-01
      相关资源
      最近更新 更多