gpt4 book ai didi

r - 使用R在一个图形上绘制多个线段

转载 作者:行者123 更新时间:2023-12-04 10:37:45 24 4
gpt4 key购买 nike

如何复制这种样式的图形,在一个图形上具有多个图,并且最好附有以下图例。

我尝试了“构面”的概念,但ggplot2和trellis:xyplot都将构面视为单独的面板,而不是覆盖的图。

我可以使用普通的Jane plot()和line()..来做,但是使用ggplot2和woudl就像是在那个包中的一个图上获取多条线。

这是一些长格式的示例数据(使用名为“Graphclick”的漂亮应用程序从图表中捕获)

comp <- read.table(pipe("pbpaste"), header=T, sep=',')

company, year, sales
Apple,1975.003,17298.457
Apple,1977.302,16784.502
Apple,1978.314,17298.457
Apple,1980.246,20730.098
Apple,1981.533,27608.426
Apple,1984.293,40862.852
Apple,1986.408,50468.617
Apple,1987.328,48236.188
Apple,1988.892,35676.547
Apple,1989.904,34616.582
Apple,1991.192,44732.742
Apple,1992.387,44732.742
Apple,1993.399,39055.324
Apple,1995.791,37894.922
Apple,1996.895,39648.746
Apple,1998.274,52804.367
Apple,1999.378,61399.512
Apple,2001.770,2.350e5
Apple,2005.265,7.735e5
Toshiba,1999.378,86856.6
Toshiba,2001.862,1.192e5
Toshiba,2004.069,1.495e5
Toshiba,2004.069,1.495e5
IBM,1975.003,22019.092
IBM,1975.830,27195.193
IBM,1976.934,30682.320
IBM,1978.130,31148.527
IBM,1980.430,35676.547
IBM,1981.625,35676.547
IBM,1983.005,39648.746
IBM,1985.305,40862.852
IBM,1986.408,46102.508
IBM,1987.512,64241.156
IBM,1989.996,75832.898
IBM,1991.100,84276.039
IBM,1992.295,85556.641
IBM,1993.307,79342.539
IBM,1994.779,79342.539
IBM,1995.791,84276.039
IBM,1996.895,95082.484
IBM,1996.895,95082.484
Commodore,1975.003,33588.051
Commodore,1975.830,34616.582
Commodore,1977.118,25219.982
Commodore,1978.130,23388.229
Commodore,1979.326,25992.234
Commodore,1980.521,21689.514
Commodore,1981.717,25219.982
Commodore,1984.201,6999.029
Commodore,1985.213,1670.460
Commodore,1986.408,1458.447

computing over time
(来源: asymco.com)

最佳答案

因此,在ggplot2中,此代码有效

qplot(year, sales, data=comp, colour=as.factor(company), group= company, geom="path", log="y")

现在剩下的唯一事情就是将Y轴上的值设置为数字格式(不是科学符号),并且标签位于图谱下的图例中,而不是在图上...欢迎提出最终建议。

最后,这比plot()+ lines()容易得多,因为这需要支持代码来获取范围,遍历组级别等。

关于r - 使用R在一个图形上绘制多个线段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com