难以创建堆叠的条形图

发布于 2025-02-12 05:09:26 字数 1567 浏览 2 评论 0原文

全新赠品

activity <- c("mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
              "mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
              "mean_fairly_active", "mean_lightly_active", "mean_lightly_active",
              "mean_lightly_active", "mean_lightly_active", "mean_lightly_active",
              "mean_lightly_active", "mean_lightly_active", "mean_very_active",
              "mean_very_active", "mean_very_active", "mean_very_active",
              "mean_very_active", "mean_very_active", "mean_very_active",
              "mean_sedentary", "mean_sedentary", "mean_sedentary", 
              "mean_sedentary", "mean_sedentary", "mean_sedentary",
              "mean_sedentary")
minutes <- c("14.3", "13.1", "12.0", "12.1", "15.2", "14.5", "14.0", "197", "190",
             "185", "204", "207", "174", "192", "23.0", "20.8", "19.4", "20.1", 
             "21.9", "20.0", "23.1", "1007", "989", "962", "1000", "964", "990", 
             "1028")
weekday <- c("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon", 
             "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed",
             "Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri",
             "Sat")
merged_means <- data.frame (activity, minutes, weekday)
print(merged_means)

ggplot(merged_means, aes(x=weekday, y=minutes, fill=activity)) +
  geom_bar(stat = "identity")

我是R的 图,按时间顺序错误的日子(周五,周六,星期六,太阳等)以及填充颜色的数量错误。 任何帮助都将受到赞赏!

I'm a total newbie to R. I'm trying to create a stacked bar chart based on this code:

activity <- c("mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
              "mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
              "mean_fairly_active", "mean_lightly_active", "mean_lightly_active",
              "mean_lightly_active", "mean_lightly_active", "mean_lightly_active",
              "mean_lightly_active", "mean_lightly_active", "mean_very_active",
              "mean_very_active", "mean_very_active", "mean_very_active",
              "mean_very_active", "mean_very_active", "mean_very_active",
              "mean_sedentary", "mean_sedentary", "mean_sedentary", 
              "mean_sedentary", "mean_sedentary", "mean_sedentary",
              "mean_sedentary")
minutes <- c("14.3", "13.1", "12.0", "12.1", "15.2", "14.5", "14.0", "197", "190",
             "185", "204", "207", "174", "192", "23.0", "20.8", "19.4", "20.1", 
             "21.9", "20.0", "23.1", "1007", "989", "962", "1000", "964", "990", 
             "1028")
weekday <- c("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon", 
             "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed",
             "Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri",
             "Sat")
merged_means <- data.frame (activity, minutes, weekday)
print(merged_means)

The ggplot I'm running:

ggplot(merged_means, aes(x=weekday, y=minutes, fill=activity)) +
  geom_bar(stat = "identity")

The resulting graph shows all the numbers of the y-axis squished on the bottom half of the graph, the days in the wrong chronological order (Fri, Mon, Sat, Sun, etc), and the fill colors in the wrong amounts.
Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2025-02-19 05:09:26

您有一些问题要解决:

  1. 您需要将分钟转换为数字,因为它是当前的字符向量,并且R不知道您打算将这些字符串解释为数字。
  2. 工作日将转换为GGPLOT内部的因子变量,因此将按字母顺序排列。为了防止这种情况,将工作日作为您选择订购的因素变量。
  3. 写作geom_bar(stat =“ Identity”)只是写作geom_col()
  4. 用于绘图目的的漫长方法,最好在不分散注意力的情况下呈现您的传奇标签,并添加适当的轴标题。
  5. 有了明确有序的因子水平,例如活动量表,您还应该使它们成为正确订购的因素。
  6. 默认样式可以调整为口味,尤其是,对于此数据集,使用顺序调色板来强调活动水平是明智的。

将所有这些放在一起,我们可能会有类似的东西:

ggplot(merged_means, 
       aes(x    = factor(weekday, unique(weekday)), 
           y    = as.numeric(minutes), 
           fill = factor(trimws(gsub("mean|_", " ", activity)),
                         c("very active",
                           "fairly active",
                           "lightly active",
                           "sedentary")))) +
  geom_col(color = "gray50") +
  scale_fill_brewer(palette = "YlOrRd", direction = -1) +
  labs(x = "Weekday", y = "Minutes", fill = "Activity") +
  theme_minimal(base_size = 16)

”在此处输入图像说明”

You have a few issues to address:

  1. You need to convert minutes to numeric, since it is currently a character vector, and R doesn't know you intend these character strings to be interpreted as numbers.
  2. The weekdays are converted to a factor variable inside ggplot, and will therefore be arranged in alphabetical order. To prevent this, make weekday a factor variable with your chosen ordering.
  3. Writing geom_bar(stat = "identity") is just a long way of writing geom_col()
  4. For plotting purposes, it's a good idea to present your legend labels without distracting underscores, and to add appropriate axis titles.
  5. With clearly ordered factor levels like the activity scale, you should also make them a factor with the correct ordering.
  6. The default styling can be adjusted to taste, and in particular, it would be sensible for this data set to use a sequential palette to emphasise the activity level.

Putting all these together we might have something like this:

ggplot(merged_means, 
       aes(x    = factor(weekday, unique(weekday)), 
           y    = as.numeric(minutes), 
           fill = factor(trimws(gsub("mean|_", " ", activity)),
                         c("very active",
                           "fairly active",
                           "lightly active",
                           "sedentary")))) +
  geom_col(color = "gray50") +
  scale_fill_brewer(palette = "YlOrRd", direction = -1) +
  labs(x = "Weekday", y = "Minutes", fill = "Activity") +
  theme_minimal(base_size = 16)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文