使用ggplot2构建折线图 [英] Constructing a line graph using ggplot2
问题描述
http://www.harding .edu / fmccown / r /#linecharts
编辑:每行由两个向量组成,如下所示:
temp = c(4,25,50,85,100)
酶活性= c(0.543,0.788,0.990,0.898,0.882)
,x轴上的临时变量和每行都有不同的颜色,因此可以区分它们。 b
编辑2:
$ $ $ $ $ $ c $ amyA = c(0.091,0.147,0.202,0.236 ,0.074)
temp = c(4,23,37,65,100)
df = data.frame(temp,amyA)
ggplot(df,aes(x = temp,y = amyA,col ='blue'))+ geom_line()
第二次编辑不会生成蓝线,图例完全错误。如果我重复使用不同数据的两个ggplot调用,则只绘制一行。
解决方案关键是要整理数据在绘图之前,在数据框中有一个因子
列,表示每组 y
值。例如:
set.seed(1)
df1 < - data.frame(e1 = sort(runif (5,0.05,0.25)),
e2 = sort(runif(5,0.05,0.25)),
e3 = sort(runif(5,0.05,0.25)),
t1 = sort(runif(5,1,100)),
t2 = sort(runif(5,1,100)),
t3 = sort(runif(5,1,100))
###重新设计它以给出一个指示组
df2 < - 的列,其中(df1,
as.data.frame(cbind(c(t1,t2,t3) ,
c(e1,e2,e3),
rep(seq(3),each = 5))
colnames(df2)< -c(temp ,活动,团队)
df2 $团队< - as.factor(df2 $ team)
$ p
$ p $ library $ ggplot
ggplot(df2,aes(x = temp, y = activity,col = team))+ geom_line()
给出:
I need to plot three lines (onto a single graph) each of which represents one lab team's data (two variables / team). Ideally, the graph should look aesthetically pleasing (hence the use of ggplot2!) yet similar in form to the line graphs shown below. I don't understand how to plot multiple lines onto a single graph using the gggplot2 library. My current knowledge of / skill with the ggplot2 library is low but I have included my fledgling efforts below.
http://www.harding.edu/fmccown/r/#linecharts
Edit: Each line is constructed from two vectors that look like this:
temp = c(4, 25, 50, 85, 100)
enzyme_activity = c(0.543, 0.788, 0.990, 0.898, 0.882)
with the temp variable on the x-axis and each line a different color so they can be differentiated.
Edit2:
amyA = c(0.091, 0.147, 0.202, 0.236, 0.074)
temp = c(4, 23, 37, 65, 100)
df = data.frame(temp, amyA)
ggplot(df, aes(x = temp, y = amyA, col = 'blue')) + geom_line()
The code within the second edit does not generate a blue line and the legend is completely wrong. If I repeat the two ggplot calls with differing data, only one line is plotted.
The key thing is to organize your data before plotting so as to have a factor
column in the data frame indicating a separate line for each set of x
, y
values. For example:
set.seed(1)
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
e2 = sort(runif(5, 0.05, 0.25)),
e3 = sort(runif(5, 0.05, 0.25)),
t1 = sort(runif(5, 1, 100)),
t2 = sort(runif(5, 1, 100)),
t3 = sort(runif(5, 1, 100))
)
### reshape this to give a column indicating group
df2 <- with(df1,
as.data.frame(cbind( c(t1, t2, t3),
c(e1, e2, e3),
rep(seq(3), each=5) )
))
colnames(df2) <- c("temp","activity","team")
df2$team <- as.factor(df2$team)
Then
library(ggplot2)
ggplot(df2, aes(x=temp, y=activity, col=team)) + geom_line()
giving:
这篇关于使用ggplot2构建折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!