使用ggplot2构建折线图

使用ggplot2构建折线图

本文介绍了使用ggplot2构建折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要绘制三条线(在一张图上),每条线代表一个实验室团队的数据(两个变量/团队)。理想情况下,该图应该看起来美观(因此使用ggplot2!),但它的形式与以下所示的线形相似。我不明白如何使用gggplot2库将多行绘制到单个图上。我目前对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调用,则只绘制一行。 解决方案关键是要整理数据在绘图之前,在数据框中有一个因子列,表示每组 x , y 值。例如: set.seed(1) df1 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 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/#linechartsEdit: 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 groupdf2 <- 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构建折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 16:44