我有一个下表,我需要绘制它来显示(x轴上的周和y轴上的百分比)。我的以下代码没有绘制任何内容,但给了我一条消息。有人能帮我解决这个问题吗?
任何帮助都是非常感谢的。
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?
发布于 2017-10-31 02:05:09
这是你想要的吗?
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")
ggplot(dfx1, aes(Week,percent, col=Year, group=1)) +
geom_point() + geom_line() +
facet_wrap(~Cty, 2, scales = "fixed")
发布于 2017-10-31 02:19:27
您可以查看其他答案,如this one,可以看到您的图中缺少group = Year
。把它加进去就会得到你想要的东西:
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'
做到这一点,如下所示:
ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
geom_line() +
facet_wrap(~Cty, 2, scales = 'free')
https://stackoverflow.com/questions/47021084
复制相似问题