设置R ggarrange图(x,y轴和图例文本大小)

ibrsph3r  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(119)

我正试图用ggarrange软件包建立一个绘图数组。
我为“载体”列中的每个元素分配了一个特定的颜色,并使用循环为“来源”列中的每个元素创建了一个圆环图。最后,使用ggarrange,我构建了一个plots数组,并为三个图设置了一个图例。

#install.packages("nycflights13")
library(nycflights13)
library(ggplot2)
library(dplyr)
library(ggpubr)

#create a color palette and assign a color to each value in "carrier"
colors <- my_colors[1:length(unique(flights$carrier))]
col_by_carrier <- setNames(colors, unique(flights$carrier))

# Vector whith the values
codigos <- c("EWR", "JFK", "LGA")

# create a donut chart for each "origin"
for (i in codigos) {
  # filtering just one "origin"
  data <- flights %>%
    filter(origin == i) %>%
    group_by(carrier) %>%
    summarize(counts = n()) %>%
    mutate(percentage = counts / sum(counts) * 100)
  
  # create and save plots
  plot_name <- paste("A_", i, sep = "")
  assign(plot_name, ggplot(data = data, aes(x = 2, y = percentage, fill = carrier)) +
           geom_col(color = "black") +
           coord_polar("y", start = 0) +
           theme(panel.background = element_blank(),
                 axis.line = element_blank(),
                 axis.text = element_blank(),
                 axis.ticks = element_blank(),
                 axis.title = element_blank()) +
           scale_fill_manual(values = col_by_carrier) +
           xlim(0.5, 2.5) +
           annotate(geom = 'text', x = 0.5, y = 0.5, label = as.character(i), size = 8, fontface = "bold") +
           theme(legend.position = "none"))
}

#create an arrange of plots with a common legend
ggarrange( A_EWR,
           A_JFK,
           A_LGA,
           col =  1,
           common.legend = TRUE,
           legend = "right",
           nrow = 1)

我尝试了很多方法:1-增加标签的字体大小。2-在X轴和Y轴上插入文本。”
我尝试使用“+ theme(legend.text = element_text(size = 20))”和许多其他方法来设置ggplots,但这是不可能的
我得到了图,但我不能设置轴y和X,我不能改变文字大小的传说

s8vozzvw

s8vozzvw1#

总的来说,创建绘图的一种更简单的方法是使用镶嵌面而不是创建单独的绘图。这样可以使用theme选项设置图例文本的字体大小。关于你的问题与轴文本我不知道什么是你想要的结果。特别是当你通过主题选项删除了轴、文本和标题时。

library(nycflights13)
library(ggplot2)
library(dplyr, warn = FALSE)

# Vector whith the values
codigos <- c("EWR", "JFK", "LGA")

data <- flights %>%
  filter(origin %in% codigos) %>%
  group_by(origin, carrier) %>%
  summarize(counts = n()) %>%
  mutate(percentage = counts / sum(counts) * 100)
#> `summarise()` has grouped output by 'origin'. You can override using the
#> `.groups` argument.

data_label <- data %>%
  distinct(origin)

ggplot(data = data, aes(x = 2, y = percentage, fill = carrier)) +
  geom_col(color = "black") +
  geom_text(
    data = data_label, aes(label = origin, fill = NULL),
    x = 0.5, y = 0.5, size = 8, fontface = "bold"
  ) +
  coord_polar("y", start = 0) +
  xlim(0.5, 2.5) +
  theme(
    panel.background = element_blank(),
    axis.line = element_blank(),
    # axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    strip.text.x = element_blank(),
    legend.text = element_text(size = 20)
  ) +
  facet_wrap(~origin)

相关问题