在R中的分组箱线图中显示中值

在R中的分组箱线图中显示中值,r,boxplot,median,R,Boxplot,Median,我已经使用ggplot2和此代码创建了箱线图 plotgraph <- function(x, y, colour, min, max) { plot1 <- ggplot(dims, aes(x = x, y = y, fill = Region)) + geom_boxplot() #plot1 <- plot1 + scale_x_discrete(name = "Blog Type") plot1 <- plot1 + la

我已经使用ggplot2和此代码创建了箱线图

plotgraph <- function(x, y, colour, min, max)
{
    plot1 <- ggplot(dims, aes(x = x, y = y, fill = Region)) +
        geom_boxplot()
    #plot1 <- plot1 + scale_x_discrete(name = "Blog Type")
    plot1 <- plot1 + labs(color='Region') + geom_hline(yintercept = 0, alpha = 0.4)
    plot1 <- plot1 + scale_y_continuous(breaks=c(seq(min,max,5)), limits = c(min, max))
    plot1 <- plot1 + labs(x="Blog Type", y="Dimension Score") + scale_fill_grey(start = 0.3, end = 0.7) + theme_grey()
    plot1 <- plot1 + theme(legend.justification = c(1, 1), legend.position = c(1, 1))
    return(plot1)
}
plot1 <- plotgraph (Blog, Dim1, Region, -30, 25)
尽管我试图在y轴上添加详细的量表,但我很难精确地确定每个箱线图的准确中位数。所以我需要在每个箱线图中打印中值。还有另一个答案,这对我不起作用,因为印刷价值不在盒子里,而是挤在中间。它将是伟大的,能够打印在中间和上方的箱线图中线。 谢谢你的帮助。 编辑:我制作一个分组图,如下所示。 添加

假设Blog是您的数据框架,则以下操作应该有效:

min <- -30 
max <- 25
meds <- aggregate(Dim1~Region, Blog, median)
plot1 <- ggplot(Blog, aes(x = Region, y = Dim1, fill = Region)) +
  geom_boxplot()
plot1 <- plot1 + labs(color='Region') + geom_hline(yintercept = 0, alpha = 0.4)
plot1 <- plot1 + scale_y_continuous(breaks=c(seq(min,max,5)), limits = c(min, max))
plot1 <- plot1 + labs(x="Blog Type", y="Dimension Score") + scale_fill_grey(start = 0.3, end = 0.7) + theme_grey()
plot1 + theme(legend.justification = c(1, 1), legend.position = c(1, 1)) +
  geom_text(data = meds, aes(y = Dim1, label = round(Dim1,2)),size = 5, vjust = -0.5, color='white')
假设Blog是您的数据框架,那么以下操作应该有效:

min <- -30 
max <- 25
meds <- aggregate(Dim1~Region, Blog, median)
plot1 <- ggplot(Blog, aes(x = Region, y = Dim1, fill = Region)) +
  geom_boxplot()
plot1 <- plot1 + labs(color='Region') + geom_hline(yintercept = 0, alpha = 0.4)
plot1 <- plot1 + scale_y_continuous(breaks=c(seq(min,max,5)), limits = c(min, max))
plot1 <- plot1 + labs(x="Blog Type", y="Dimension Score") + scale_fill_grey(start = 0.3, end = 0.7) + theme_grey()
plot1 + theme(legend.justification = c(1, 1), legend.position = c(1, 1)) +
  geom_text(data = meds, aes(y = Dim1, label = round(Dim1,2)),size = 5, vjust = -0.5, color='white')

谢谢我的数据框是dims对象,我在这个函数之前附加它,然后从中传递一个不同的变量来同时创建多个绘图。然后你需要使用aes_字符串,我猜aes将不起作用。你能看看我的问题吗?谢谢,谢谢。我的数据框是dims对象,我在这个函数之前附加它,然后从中传递一个不同的变量来同时创建多个绘图。然后你需要使用aes_字符串,我猜aes将不起作用。你能看看我的问题吗?谢谢,谢谢。因为我传递了我的变量。使用附加变量的一部分,而不是传递给变量x,y,使此代码可重用,我需要同时创建4个图形。也许你提到了同样的非标准评估。我目前正在努力学习和理解这个术语的含义。这将意味着使用aes_字符串作为ggplot函数,并在创建数据帧的dplyr函数中进行mutate_,如果我们要使其真正起作用的话。因此,我将dplyr代码放在函数dims%group_by_x,color%>%mutate_med=mediany中,添加相应的字符串函数。类似地,两个aes函数也是_string:plot1 I将其组合在一个函数中。它对我很有用。@MuhammadShakirAziz:我不再在函数中使用dplyr,因为我对它有一些无法预料的问题。使用Reforme2,这是一种将宽格式转换为长格式的简单方法,我避开了这些问题。谢谢。因为我传递了我的变量。使用附加变量的一部分,而不是传递给变量x,y,使此代码可重用,我需要同时创建4个图形。也许你提到了同样的非标准评估。我目前正在努力学习和理解这个术语的含义。这将意味着使用aes_字符串作为ggplot函数,并在创建数据帧的dplyr函数中进行mutate_,如果我们要使其真正起作用的话。因此,我将dplyr代码放在函数dims%group_by_x,color%>%mutate_med=mediany中,添加相应的字符串函数。类似地,两个aes函数也是_string:plot1 I将其组合在一个函数中。它对我很有用。@MuhammadShakirAziz:我不再在函数中使用dplyr,因为我对它有一些无法预料的问题。使用Reforme2,这是一种将宽格式转换为长格式的简单方法,我避开了这些问题。
library(dplyr)
dims=dims%>%
  group_by(Blog,Region)%>%
  mutate(med=median(Dim1))
plotgraph <- function(x, y, colour, min, max)
{
  plot1 <- ggplot(dims, aes(x = x, y = y, fill = Region)) +
    geom_boxplot()+
    labs(color='Region') + 
    geom_hline(yintercept = 0, alpha = 0.4)+
    scale_y_continuous(breaks=c(seq(min,max,5)), limits = c(min, max))+
    labs(x="Blog Type", y="Dimension Score") + scale_fill_grey(start = 0.3, end = 0.7) + 
    theme_grey()+
    theme(legend.justification = c(1, 1), legend.position = c(1, 1))+
    geom_text(aes(y = med,x=x, label = round(med,2)),position=position_dodge(width = 0.8),size = 3, vjust = -0.5,colour="blue")
  return(plot1)
}
plot1 <- plotgraph (Blog, Dim1, Region, -30, 25)
library(ggplot2)
library(reshape2)
plotgraph <- function(x, y, colour, min, max)
{
  plot1 <- ggplot(dims, aes_string(x = x, y = y, fill = colour)) +
    geom_boxplot()+
    labs(color=colour) + 
    geom_hline(yintercept = 0, alpha = 0.4)+
    scale_y_continuous(breaks=c(seq(min,max,5)), limits = c(min, max))+
    labs(x="Blog Type", y="Dimension Score") +
    scale_fill_grey(start = 0.3, end = 0.7) + 
    theme_grey()+
    theme(legend.justification = c(1, 1), legend.position = c(1, 1))+
    geom_text(data= melt(with(dims, tapply(eval(parse(text=y)),list(eval(parse(text=x)),eval(parse(text=colour))), median)),varnames=c("Blog","Region"),value.name="med"),
              aes_string(y = "med",x=x, label = "med"),position=position_dodge(width = 0.8),size = 3, vjust = -0.5,colour="blue")
  return(plot1)
}
plot1 <- plotgraph ("Blog", "Dim1", "Region", -30, 25)