【问题标题】:Populate facet labels from another dataframe in facet_wrap ggplot2从 facet_wrap ggplot2 中的另一个数据框填充构面标签
【发布时间】:2018-07-13 23:52:43
【问题描述】:

我正在尝试重命名我的情节中的构面标签,我必须从另一个数据框中读取它。我已经做了一个函数来检索每个业务的正确标签,但是所有标签都打印在构面中,这是什么问题?

df <- structure(list(Year_Month = c(
  "2016_06", "2016_06", "2016_07", 
  "2016_07", "2016_08", "2016_08", "2016_09", "2016_09", "2016_09", 
  "2016_09", "2016_10", "2016_10", "2016_10", "2016_10", "2016_11", 
  "2016_11", "2016_12", "2016_12", "2017_01", "2017_01", "2017_01", 
  "2017_02", "2017_02", "2017_02", "2017_02", "2017_03", "2017_03", 
  "2017_03", "2017_03", "2017_03", "2017_03", "2017_04", "2017_04", 
  "2017_04", "2017_04", "2017_04", "2017_05", "2017_05", "2017_05", 
  "2017_05", "2017_05", "2017_05", "2017_06", "2017_06", "2017_06", 
  "2017_06", "2017_06", "2017_06", "2017_07", "2017_07"), 
  Business = c("A", 
               "E", "A", "B", "B", "E", "F", "A", "H", "B", "A", "D", "B", "E", 
               "B", "E", "F", "B", "F", "B", "E", "A", "B", "C", "E", "F", "A", 
               "G", "D", "B", "E", "F", "A", "G", "B", "E", "F", "A", "D", "B", 
               "C", "E", "F", "A", "D", "B", "C", "E", "F", "A"),
  `MMR Count` = c(2L, 
                  1L, 1L, 7L, 2L, 1L, 1L, 3L, 1L, 5L, 1L, 1L, 4L, 1L, 8L, 4L, 1L, 
                  4L, 2L, 2L, 2L, 3L, 8L, 1L, 2L, 1L, 7L, 1L, 4L, 9L, 2L, 4L, 10L, 
                  2L, 15L, 7L, 4L, 27L, 2L, 14L, 1L, 6L, 9L, 31L, 5L, 14L, 1L, 
                  4L, 5L, 21L),
  `Duration Average` = c(37, 20, 9, 8, 2, 5, 1, 1, 
                         1, 14, 1, 19, 8, 1, 21, 77, 1, 18, 8, 1, 1, 194, 9, 14, 19, 1, 
                         10, 1, 6, 9, 18, 4, 12, 170, 7, 35, 9, 10, 7, 12, 3, 15, 5, 9, 
                         10, 10, 18, 11, 16, 14)), .Names = c("Year_Month", "Business", 
                                                              "MMR Count", "Duration Average"), row.names = c(NA, 50L), class = "data.frame")

df$date = as.Date(paste0(df$Year_Month, "_01"), format = "%Y_%m_%d")


#calculating the mean of data
library(tidyverse)
dMean <- df %>%
  group_by(Business) %>%
  summarise(MN = round(mean(`Duration Average`),2)) %>%
  mutate(label=paste(Business, " (Overall: ",MN,")"))


#function to retrieve the label of a specific business to be shown on the facet
get_mean <-function(string) {
  d_label <- dMean %>% filter(Business==string) %>% select(label);
  d_label= as.character(d_label)
  return (d_label)
}

appender <- function(string, suffix = get_mean(string)) paste0(string, suffix)


#plot
ggplot(df,
       aes(x=date,
           y=`Duration Average`,
           group=Business,
           color=Business,
           size=`MMR Count`)) +
  geom_line(aes(group=Business),stat="identity", size=1, alpha=0.7) +
  geom_point(aes(colour=Business, alpha=0.7)) +
  facet_wrap(~ Business, ncol=2, labeller = as_labeller(appender)) +
  scale_y_log10( limits=c(.1,1000),breaks=c(1,10,100,1000)) +   
  scale_alpha_continuous(range = c(0.5,1), guide='none') + 
  geom_text(aes(label=`Duration Average`,vjust=-1),size=3) +
  geom_text(aes(label=`MMR Count`,vjust=2),size=3,color="brown")

【问题讨论】:

  • 那么想要的输出是什么?在寻求帮助时,您应该包含一个具有所需输出的最小可重现示例。删除与您的问题没有直接关系的任何内容。滚动浏览的代码更少,更容易回答您的问题。
  • 我分享的是可重现的示例,而不是原始问题。如果您查看 dMEAN 数据框,我希望列 label 成为构面的标签

标签: r ggplot2 facet-wrap


【解决方案1】:

一种选择是将dMean 加入df,然后按标签分面

df %>% 
  left_join(dMean) %>% 
  ggplot(aes(x=date,
             y=`Duration Average`,
            group=1,
             color=Business,
             size=`MMR Count`
           )) +
  geom_line(size = 1, alpha=0.7) +
  geom_point(alpha=0.7) +
  facet_wrap(~ label, ncol=2) +
  scale_y_log10( limits=c(.1,1000),breaks=c(1,10,100,1000)) +   
  scale_alpha_continuous(range = c(0.5,1), guide='none') + 
  geom_text(aes(label=`Duration Average`,vjust=-1),size=3) +
  geom_text(aes(label=`MMR Count`,vjust=2),size=3,color="brown")

【讨论】:

  • 我已经这样做了,它已经工作了,但我不想扭曲 df 中的业务列
  • 您能否详细说明“扭曲业务专栏”的含义?因为将dMean 加入df 不会影响业务列。查看df2 &lt;- df %&gt;% left_join(dMean),然后查看identical(df$Business, df2$Business)
  • 是的,我只是注意到代码没有处理业务列,而是使用连接数据框中的标签列,但是我在成功运行它时遇到问题,我在 rdrr 上尝试过。 io,它给了我这个错误:Error in combine_vars(data, params$plot_env, vars, drop = params$drop) : At least one layer must contain all variables used for facetting
  • 确保你的面部参数看起来像这样facet_wrap(.~ label, ncol=2)
  • 尝试删除句点facet_wrap(~ label, ncol=2)
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 2020-11-05
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多