我有这样的数据框:
frame <- data.frame("AGE" = seq(18,44,1),
"GROUP1"= c(83,101,159,185,212,276,330,293,330,356,370,325,264,274,214,229,227,154,132,121,83,69,57,32,16,17,8),
"GROUP2"= c(144,210,259,329,391,421,453,358,338,318,270,258,207,186,173,135,106,92,74,56,41,31,25,13,16,5,8))
我想在不同颜色的相同图中绘制X轴上的AGE和Y轴上的GROUP1和GROUP2的值。并且值应该由平滑的线连接。
作为第一部分,我融化了数据框并绘制了:
melt <- melt(frame, id.vars = "AGE")
melt <- melt[order(melt$AGE),]
plot(melt$AGE, melt$value)
答案 0 :(得分:1)
以下是使用tidyr
和library(dplyr)
library(tidyr)
newframe <- frame %>% gather("variable","value",-AGE)
ggplot(newframe, aes(x=AGE, y=value, color=variable)) +
geom_point() +
geom_smooth()
软件包的替代解决方案。
geom_line()
您可以使用geom_smooth()
来获取各点之间的界限,但在此处使用geom_area
会感觉更好。 color
为您提供了一行下方的阴影区域,但我们需要将fill
更改为ggplot(newframe, aes(x=AGE, y=value, fill=variable)) + geom_area()
。
{% load staticfiles %}
some more --like header , section
<footer>
<div id="footer">
{% block footer %}
<a href="https://github.com/shanker4999"> <img src ="../../static/blog/images/git.png"></a>
<p>© 2016 shankar.</p>
{% endblock %}
</div>
答案 1 :(得分:0)
我们可以使用matplot
matplot(`row.names<-`(as.matrix(frame[-1]), frame[,1]),
ylab='value',type = "l", xlab = "AGE",col = c("red", "blue"), pch = 1)
legend("topright", inset = .05, legend = c("GROUP1", "GROUP2"),
pch = 1, col = c("red", "blue"), horiz = TRUE)
答案 2 :(得分:0)
尝试,
library(ggplot2)
ggplot(meltdf,aes(x=AGE,y=value,colour=variable,group=variable)) + geom_line()