难以创建堆叠的条形图
全新赠品
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一些问题要解决:
分钟
转换为数字,因为它是当前的字符向量,并且R不知道您打算将这些字符串解释为数字。工作日
作为您选择订购的因素变量。geom_bar(stat =“ Identity”)
只是写作geom_col()
将所有这些放在一起,我们可能会有类似的东西:
You have a few issues to address:
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.weekday
a factor variable with your chosen ordering.geom_bar(stat = "identity")
is just a long way of writinggeom_col()
Putting all these together we might have something like this: