简体   繁体   中英

How to fit stat_summary to rectangle in ggplot?

I want to fit text from stat_summary to a rectangle in ggplot, so it adjusts automatically according to available space.

在此处输入图片说明

library(ggplot2)
category <- c('a','b','a','b','c','a','c')
values <- c(10,20,30,10,10,20,40)
dataset <- data.frame(category,values)
z = max(values)
stat_box_data <- function(y, upper_limit = z + 40) {
  return( 
    data.frame(
      y = upper_limit,
      label = paste('Contagem =', paste(length(y)), '\n',
                    'Média =', paste(round(mean(y), 1),'horas'), '\n',
                    'Desvio =', paste(round(sd(y), 1),'horas'), '\n',
                    'Mínimo =', paste(round(min(y),1),'horas'), '\n',
                    'Máximo =', paste(round(max(y),1),'horas'), '\n',
                    'Mediana =', paste(round(median(y),1),'horas'), '\n',
                    'Total =', paste(round(sum(y),1),'horas'), '\n'
      )
    )
  )
}
ggplot(dataset,aes(x=category, y=values))+
  geom_rect(aes(x=category, y=values),xmin = 0, xmax = 10, ymin = z+10, ymax = z+50,col = 'white') +
geom_boxplot(aes(x=category, y=values))+
stat_summary(fun.data = stat_box_data, geom = "text", hjust = 0.5,vjust = 0.85) +

geom_point(aes(x=category, y=values))+
facet_grid(. ~ category, space = "free", scales="free", margins=TRUE)

Is there a way for me to it?

There's this package: https://github.com/wilkox/ggfittext , but I can't make it work with stat_summary!

The most recent version of ggfittext (version 0.8.1) makes this a lot easier. Once this is installed, it becomes quite straightforward to have the text automatically sized the way you want:

library(ggplot2)
library(ggfittext)
packageVersion("ggfittext")
#> [1] '0.8.1'

category <- c('a','b','a','b','c','a','c')
values <- c(10,20,30,10,10,20,40)
dataset <- data.frame(category,values)
z = max(values)
stat_box_data <- function(y, upper_limit = z + 40) {
  return( 
    data.frame(
      y = upper_limit,
      label = paste('Contagem =', paste(length(y)), '\n',
                    'Média =', paste(round(mean(y), 1),'horas'), '\n',
                    'Desvio =', paste(round(sd(y), 1),'horas'), '\n',
                    'Mínimo =', paste(round(min(y),1),'horas'), '\n',
                    'Máximo =', paste(round(max(y),1),'horas'), '\n',
                    'Mediana =', paste(round(median(y),1),'horas'), '\n',
                    'Total =', paste(round(sum(y),1),'horas'), '\n'
      )
    )
  )
}

ggplot(dataset,aes(x = category, y = values))+
  geom_rect(xmin = 0, xmax = 10, ymin = 50, ymax = 80, col = "white") +
  geom_boxplot() +
  geom_fit_text(stat = "summary", fun.data = "stat_box_data", ymin = 50, ymax = 80) +
  geom_point() +
  facet_grid(. ~ category, space = "free", scales = "free", margins = TRUE)

Created on 2019-07-20 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM