如何ggplot部分值与晶须图只有最大值? [英] How to ggplot partial values with only max in whisker plot?

查看:384
本文介绍了如何ggplot部分值与晶须图只有最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑如何在whisker plot中显示部分值...
M2 只有最大值。
这两种测量都不具有
图1中输出的代码

  library(reshape2) 
library(ggplot2)

ds < - structure(list(Vars = c(M1,M2,M1,M2,M1, M2),
variable = structure(c(1L,1L,2L,2L,3L,3L),.Label = c(Max,
Ave,Min class =factor),value = c(150,
61,60,NA,41,NA)),row.names = c(NA,-6L) Names = c(Vars,
variable,value),class =data.frame)

#http://stackoverflow.com/q/44100187/ 54964 eipi10
ds $ value = as.numeric(ds $ value)

#http://stackoverflow.com/a/44090815/54964
minmax< - ds [ ds $ variable%in%c(Min,Max),]
absol <-ds [ds $ variable%in%c(Ave),]
#absol< - ds [ds $ variable%in%c(Ave,Absolute),]
minm <-dcast(minmax,Vars_variable)
absol < - merge(absol,minm ,by =Vars,all.x = T)

absol

ggplot(absol,aes(x = Vars,y = value,fill = variable))+
geom_bar(stat =identity)+
geom_errorbar(aes(ymin = Min,ymax = Max),width = .25)

开始时的值

  Max Ave Min Vars 
M1 150 60 41 M1
M2 61< NA> < NA> M2

1输出,当只有最大值时不可视化





M1的呈现在barplot中也很奇怪,因为数据中没有绝对值,最初设计在 absol 中。

预期产出:在 M2中标记最大值介绍

<操作系统:Debian 8.7

R:3.4(backports)

解决方案

absol ,称它为 yMin ,如果最小值丢失,它将设置最小值为最大值。

  absol $ yMin < -  ifelse(is.na(absol $ Min),absol $ Max,absol $ Min)

然后,当绘图时,让 geom_errorbar 使用<$ c

pre $ g $ p $ ggplot(absol,aes(x = Vars,y)

= value,fill = variable))+
geom_bar(stat =identity)+
geom_errorbar(aes(ymin = yMin,ymax = Max),width = .25)


I am thinking how to present partial values in whisker plot/... M2 has only the max. Both measurements do not hav Code which output in Fig. 1

library("reshape2")
library("ggplot2")

ds <- structure(list(Vars = c("M1", "M2", "M1", "M2", "M1", "M2"), 
variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Max", 
"Ave", "Min"), class = "factor"), value = c("150", 
"61", " 60", NA, " 41", NA)), row.names = c(NA, -6L), .Names = c("Vars", 
"variable", "value"), class = "data.frame")

# http://stackoverflow.com/q/44100187/54964 eipi10
ds$value = as.numeric(ds$value)

# http://stackoverflow.com/a/44090815/54964
minmax <- ds[ds$variable %in% c("Min","Max"), ]
absol  <- ds[ds$variable %in% c("Ave"), ]
# absol  <- ds[ds$variable %in% c("Ave", "Absolute"), ]
minm   <- dcast(minmax, Vars ~ variable)
absol  <- merge(absol, minm, by = "Vars", all.x = T)

absol

ggplot(absol, aes(x = Vars, y = value, fill = variable)) +
        geom_bar(stat = "identity") +
        geom_errorbar(aes(ymin = Min, ymax = Max), width = .25)

Values at start

            Max      Ave       Min Vars
M1          150       60        41   M1
M2           61     <NA>      <NA>   M2

Fig. 1 Output where no visualisations when only max value exists

The presentation of M1 is also weird in the barplot becuase no absolute values in data, designed initially in absol.

Expected output: mark maximum value in M2 presentation

OS: Debian 8.7
R: 3.4 (backports)

解决方案

Add a column to absol, call it yMin, that will set the min value to the max value if the min value is missing.

absol$yMin <- ifelse(is.na(absol$Min), absol$Max, absol$Min)

Then, when plotting, have the geom_errorbar use yMin in the aesthetics.

ggplot(absol, aes(x = Vars, y = value, fill = variable)) +
        geom_bar(stat = "identity") + 
        geom_errorbar(aes(ymin = yMin, ymax = Max), width = .25)

这篇关于如何ggplot部分值与晶须图只有最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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