-1

我需要帮助才能使用 mtcars$mpg 和 mtcars$carb 创建饼图。我想做这个; 我想创建一个饼图,显示每个化油器的总 mpg 值。让我解释更多;在饼图中,每个切片会根据总mpg值显示化油器的值(1、2、3、4、6、8)和形状。我写了一些命令,但我怎样才能用这些创建一个 tablo,我应该如何继续?我需要最好的简单方法。请帮我。谢谢...

> carb1 <- filter(mtcars, carb==1)
> carb2 <- filter(mtcars, carb==2)
> carb3 <- filter(mtcars, carb==3)
> carb4 <- filter(mtcars, carb==4)
> carb6 <- filter(mtcars, carb==6)
> carb8 <- filter(mtcars, carb==8)
> summpg_carb1 <- sum(carb1$mpg)
> summpg_carb2 <- sum(carb2$mpg)
> summpg_carb3 <- sum(carb3$mpg)
> summpg_carb4 <- sum(carb4$mpg)
> summpg_carb6 <- sum(carb6$mpg)
> summpg_carb8 <- sum(carb8$mpg)
4

3 回答 3

2

使用ggplot2plotly

# Install pacakges if they are not already installed: necessary_packages => vector
necessary_packages <- c("ggplot2", "plotly")

# Create a vector containing the names of any packages needing installation:
# new_pacakges => vector
new_packages <- necessary_packages[!(necessary_packages %in%
                                       installed.packages()[, "Package"])]

# If the vector has more than 0 values, install the new pacakges
# (and their) associated dependencies:
if(length(new_packages) > 0){install.packages(new_packages, dependencies = TRUE)}

# Initialise the packages in the session: list of boolean => stdout (console)
lapply(necessary_packages, require, character.only = TRUE)

# Aggregate the data.frame: 
agg_df <- transform(aggregate(mpg ~ carb, mtcars, sum),
                    carb = as.factor(paste(
                      carb, paste0(round(prop.table(mpg), 4) * 100, "%"),
                      sep = " - "
                    )))

# Chart aggregated data.frame: 
ggplot(agg_df, aes(x = "", y = mpg, fill = carb)) +
  geom_bar(width = 1, stat = "identity") +
  scale_fill_viridis_d(option = "viridis") +
  coord_polar("y", start = 0) +
  ylab("") +
  xlab("Total MPG") +
  ggtitle("Total MPG by Carburetor") +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank()
  ) 

# Plotly chart: 
plot_ly(aggregate(mpg ~ carb, mtcars, sum),
        labels = ~sort(carb), values = ~mpg, type = "pie",
        marker = list(colors=c("#440154FF", "#404788FF", "#2E6E8EFF", "#20A486FF", "#44BF70FF",
                               "#FDE725FF", "#20A387FF")),
        textinfo = "label+percent",
        textposition = "outside") %>% 
  layout(title = "Total MPG by Carburetor")
于 2020-12-27T14:06:38.757 回答
2

您尝试在额外的代码行中对 6 个值中的每一个值求和的尝试mtcars$carb并不大,而且容易出错并且整体风格不好。在 R 中有多种方法可以聚合数据,其中包括函数aggregate

aggr <- aggregate(mtcars$mpg, list(mtcars$carb), sum)
print(aggr)
pie(aggr$x, aggr$Group.1)

by功能(在这种特殊情况下甚至更全面):

b <- by(mtcars$mpg, mtcars$carb, sum)
pie(b, names(b))
于 2020-12-27T13:17:20.137 回答
0

这类问题往往会在 stackoverflow 上得到三个不同的答案。一种用于标准 R,一种用于 R 增强通孔dplyr,另一种用于 R 增强通孔data.table封装。第一dplyr反应是 stefan 的评论。为了完整起见,这是一个data.table四舍五入的答案。data.table倾向于为那些参与其中的人提供最短的代码,并且通常是运行速度最快的代码,这对于像 mtcars 这样的小型数据集没有价值。

library(data.table)

mtcars.dt <- data.table(mtcars)
aggr <- mtcars.dt[,sum(mpg), carb][order(carb),]
pie(x = aggr$V1, labels = aggr$carb)
aggr

您上面的“手动”方法翻译data.table为完整示例,可能如下所示:

library(data.table)
mtcars.dt <- data.table(mtcars)
aggr <- c( mtcars.dt[carb == 1, sum(mpg)],
           mtcars.dt[carb == 2, sum(mpg)],
           mtcars.dt[carb == 3, sum(mpg)],
           mtcars.dt[carb == 4, sum(mpg)],
           mtcars.dt[carb == 6, sum(mpg)],
           mtcars.dt[carb == 8, sum(mpg)]) 
perc <- 100 * round(aggr / sum(aggr), 3)
           
pie(aggr, labels = paste(perc, "%"), col = rainbow(6))
legend("topright", fill = rainbow(6), legend = c(1, 2, 3, 4, 6, 8), title = "carb")
于 2020-12-27T13:56:30.770 回答