简体   繁体   English

仅绘制大于给定值的值:ggplot R

[英]Plot only values greater than given value: ggplot R

With help from @Nick Criswell I have learned how to create a make plot function and lapply using ggplot2 and aes_string . 在@Nick Criswell的帮助下,我学习了如何创建make plot函数以及如何使用ggplot2aes_string进行应用 loop Freq plot with GGPLOT aes_string GGPLOT aes_string循环频率图

I am having trouble modifying the code to only plot values greater than a given threshold. 我在修改代码以仅绘制大于给定阈值的值时遇到麻烦。 Before using the function approach I coded the following that worked: 在使用函数方法之前,我对以下代码进行了编码:

p <- ggplot(subset(DAT, al.sum>10), aes(word, al.sum, y=freq)) 

Question: how do I modify the following code to only plot values over a given threshold? 问题:如何修改以下代码以仅绘制给定阈值上的值? ie I only want each record to be included in the frequency plot if the count is greater than 10. From the code below with this condition for al.sum only Dot_Matricks would be included in the plot. 也就是说,如果计数大于10,我只希望将每个记录包含在频率图中。从下面的代码中,对于al.sum,此条件仅将Dot_Matricks包括在频率图中。

#set work DIR
setwd("C:/A")

#Data create 
DAT <- read.table(text = "ID  a1.sum  b3.sum  c6.sum  d9.sum
April_Showers   10  5   15  0
              Anita_Job   2   3   1   14
              Candy_Cain  4   7   14  17
              Crystal_Ball    6   8   16  12
              Dot_Matricks    15  9   0    1
              Kay_Largo   4   10  5   13", 
              header = TRUE, stringsAsFactors = FALSE)

 #Plot Data function  
 library(ggplot2)

 make_plots = function(data, column){
 ggplot(data, aes_string(x = "ID", y=column)) +
 geom_bar(stat="identity", fill="blue", color="green") +
 theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = "white"),        
      panel.grid.major = element_line(colour = "white",size=0.25),
      panel.grid.minor = element_blank(),
      axis.text.x=element_text(size=10,angle=90, hjust=1, 
                               face="plain", family="serif"),
      axis.text.y=element_text(size=10, hjust=1, face="plain", family="serif"), 
      axis.line.x = element_line(color="black", size = 0.50), 
      axis.line.y = element_line(color="black", size = 0.5))
}

#lapply function 
myplots <- lapply(colnames(DAT[2:ncol(DAT)]), make_plots, data = DAT)

#collect names of each col that end with .sum
n <- names(DAT[grep("*.sum",names(DAT))])

#save each plot as .png
for (i in 1:length(n)){
  print(myplots[i])
ggsave(filename=paste0(i,".png"))
}

This should do the trick: 这应该可以解决问题:

make_plots = function(data, column,Threshold){

  PltData <- data[data[,column] >Threshold,]

  Plt <- ggplot(PltData, aes_string(x = "ID", y=column)) +
    geom_bar(stat="identity", fill="blue", color="green") +
    theme_bw()+
    theme(
      panel.grid.major = element_line(colour = "white",size=0.25),
      panel.grid.minor = element_blank(),
      axis.text.x=element_text(size=10,angle=-30, hjust=0)
    )
  # Plt
  ggsave(Plt,filename=paste0(column,".png"))
}

lapply(colnames(DAT[2:ncol(DAT)]), make_plots, data = DAT,Threshold=10)

You could do this with the subset function as well. 您也可以使用子集功能执行此操作。 If you want to return the plots to your R session comment the ggsave line and uncomment Plt . 如果要将图返回到R会话,请注释ggsave行并取消注释Plt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只显示 R plotly 中值大于 0 的类别? - How to show only categories with values greater than 0 in R plotly? 计算大于r中数组列中每个值的值数 - count the number of values greater than each value in a column of an array in r 仅当 ggplot 中的值 A 大于值 B 时,如何对两个时间序列之间的区域进行着色? - How to shade the area between two time series only if value A is greater than value B in ggplot? R: ggplot: two plots in one dataframe: color one plot only when it is less than the other plot - R: ggplot: two plots in one dataframe: color one plot only when it is less than the other plot 使用ggplot2,如何创建直方图或条形图,其中最后一个条形是所有值大于某个数字的计数? - Using ggplot2, how can I create a histogram or bar plot where the last bar is the count of all values greater than some number? 如何找到包含大于或等于给定数字的值的 R 矩阵的列数? - How to find the number of columns of an R matrix that contains values greater than or equal to a given number? R:仅 Select 值大于某个值并映射到值为是或否的另一列的行 - R: Select only Rows where value greater than a certain value and Mapped to another column where value is Yes or No logit的R代码生成大于1的值? - R code for logit generates values greater than 1? R中大于0的值的随机游动总和 - Random walk sum of values greater than 0 in R 在 r 中用 1 替换大于零的值 - Replace values greater than zero with 1 in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM