从一个精简版创建多个条形图

时间:2023-02-01 22:54:25

I want to know if there is an efficient way of producing smaller barplots from one table. My aim is to produce clean graphs rather than one dense graph that can hardly be read. Is there a way to do this without extesive coding? The source table is in a data.frame object type.

我想知道是否有一种从一张桌子生产较小条形图的有效方法。我的目标是生成干净的图形,而不是一个难以读取的密集图形。有没有办法在没有繁琐的编码的情况下做到这一点?源表是data.frame对象类型。

1 个解决方案

#1


2  

Here are four different plots, perhaps one of these is to your liking.

这里有四个不同的情节,也许其中一个是你喜欢的。

library(ggplot2)  # plotting and the diamonds data set
library(dplyr)    # needed for the filter function


# Unwanted 'dense' graph
g1 <- 
  ggplot(diamonds) + 
  aes(x = cut, fill = color) + 
  geom_bar() + 
  ggtitle("g1: stacked bar plot")

从一个精简版创建多个条形图

# or
g2 <- 
  ggplot(diamonds) + 
  aes(x = cut, fill = color) + 
  geom_bar(position = position_dodge()) + 
  ggtitle("g2: dodged bar plot")

从一个精简版创建多个条形图

# different option, layered bars 
g3 <- 
  ggplot() + 
    aes(x = cut, fill = color) + 
    geom_bar(data = filter(diamonds, color == "D"), width = 0.90) +
    geom_bar(data = filter(diamonds, color == "E"), width = 0.77) + 
    geom_bar(data = filter(diamonds, color == "F"), width = 0.63) + 
    geom_bar(data = filter(diamonds, color == "G"), width = 0.50) + 
    geom_bar(data = filter(diamonds, color == "H"), width = 0.37) + 
    geom_bar(data = filter(diamonds, color == "I"), width = 0.23) + 
    geom_bar(data = filter(diamonds, color == "J"), width = 0.10) +
    ggtitle("g3: overlaid bar plot")

从一个精简版创建多个条形图

# facet plot
g4 <- 
  ggplot(diamonds) + 
  aes(x = cut) + 
  geom_bar() + 
  facet_wrap( ~ color)

从一个精简版创建多个条形图

#1


2  

Here are four different plots, perhaps one of these is to your liking.

这里有四个不同的情节,也许其中一个是你喜欢的。

library(ggplot2)  # plotting and the diamonds data set
library(dplyr)    # needed for the filter function


# Unwanted 'dense' graph
g1 <- 
  ggplot(diamonds) + 
  aes(x = cut, fill = color) + 
  geom_bar() + 
  ggtitle("g1: stacked bar plot")

从一个精简版创建多个条形图

# or
g2 <- 
  ggplot(diamonds) + 
  aes(x = cut, fill = color) + 
  geom_bar(position = position_dodge()) + 
  ggtitle("g2: dodged bar plot")

从一个精简版创建多个条形图

# different option, layered bars 
g3 <- 
  ggplot() + 
    aes(x = cut, fill = color) + 
    geom_bar(data = filter(diamonds, color == "D"), width = 0.90) +
    geom_bar(data = filter(diamonds, color == "E"), width = 0.77) + 
    geom_bar(data = filter(diamonds, color == "F"), width = 0.63) + 
    geom_bar(data = filter(diamonds, color == "G"), width = 0.50) + 
    geom_bar(data = filter(diamonds, color == "H"), width = 0.37) + 
    geom_bar(data = filter(diamonds, color == "I"), width = 0.23) + 
    geom_bar(data = filter(diamonds, color == "J"), width = 0.10) +
    ggtitle("g3: overlaid bar plot")

从一个精简版创建多个条形图

# facet plot
g4 <- 
  ggplot(diamonds) + 
  aes(x = cut) + 
  geom_bar() + 
  facet_wrap( ~ color)

从一个精简版创建多个条形图