首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在R中包含分类和数值的ggplot子图

在R中包含分类和数值的ggplot子图
EN

Stack Overflow用户
提问于 2017-10-31 01:32:19
回答 2查看 169关注 0票数 0

我有一个下表,我需要绘制它来显示(x轴上的周和y轴上的百分比)。我的以下代码没有绘制任何内容,但给了我一条消息。有人能帮我解决这个问题吗?

任何帮助都是非常感谢的。

代码语言:javascript
运行
复制
dfx1:

Year  State  Cty        Week   ac_sum    percent
1998  KS     Coffey     10-1   79        6.4
1998  KS     Coffey     10-3   764       62
1998  KS     Coffey     10-4   951       77.2
1998  KS     Coffey     10-5   1015      82.4
1998  KS     Coffey     11-2   1231      100
1998  KS     Crawford   10-3   79        6.1
1998  KS     Crawford   10-4   764       15.8
1998  KS     Crawford   10-5   951       84.1
1998  KS     Crawford   11-2   1015      100
.
.
.
.


gg <- ggplot(dfx1, aes(Week,percent, col=Year))
gg <- gg + geom_line()
gg <- gg + facet_wrap(~Cty, 2, scales = "fixed")
gg <- gg + xlim(c(min(dfx1$Week), max(dfx1$Week)))
plot(gg)

geom_path: Each group consists of only one observation. Do you need to
adjust the group aesthetic?
EN

回答 2

Stack Overflow用户

发布于 2017-10-31 02:05:09

这是你想要的吗?

代码语言:javascript
运行
复制
dfx1 <- read.table(text="Year  State  Cty        Week   ac_sum    percent
1998  KS     Coffey     10-1   79        6.4
1998  KS     Coffey     10-3   764       62
1998  KS     Coffey     10-4   951       77.2
1998  KS     Coffey     10-5   1015      82.4
1998  KS     Coffey     11-2   1231      100
1998  KS     Crawford   10-3   79        6.1
1998  KS     Crawford   10-4   764       15.8
1998  KS     Crawford   10-5   951       84.1
1998  KS     Crawford   11-2   1015      100", header=T)

library(ggplot2)
ggplot(dfx1, aes(Week,percent, col=Year)) + 
    geom_point() +
    facet_wrap(~Cty, 2, scales = "fixed") 

代码语言:javascript
运行
复制
ggplot(dfx1, aes(Week,percent, col=Year, group=1)) + 
    geom_point() + geom_line() +
    facet_wrap(~Cty, 2, scales = "fixed") 

票数 1
EN

Stack Overflow用户

发布于 2017-10-31 02:19:27

您可以查看其他答案,如this one,可以看到您的图中缺少group = Year。把它加进去就会得到你想要的东西:

代码语言:javascript
运行
复制
library(ggplot2)

dfx1$Week <- factor(dfx1$Week, ordered = T)

ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
  geom_line() +
  facet_wrap(~Cty, 2, scales = 'fixed')

在您的最后一行中,您似乎只想显示实际包含数据的Week。您可以使用scales = 'free'做到这一点,如下所示:

代码语言:javascript
运行
复制
ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
  geom_line() +
  facet_wrap(~Cty, 2, scales = 'free')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47021084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档