gpt4 book ai didi

R 绘图 - 无法更改点(获取 "minus"标志)

转载 作者:行者123 更新时间:2023-12-02 03:35:30 25 4
gpt4 key购买 nike

我想绘制三个不同地点的风速,x = 日期,y = 风速。

我想要的情节基本上应该是这样的:

enter image description here除了我想要完整的日期,而不仅仅是 x 轴上的日期。上图只是一种解决方法,因为我想使用的代码会产生如下所述的故障。

数据:

> u10
date u10.TXL u10.MF u10.THF
1 2013-05-01 2.9 2.0 3.5
2 2013-05-02 3.1 2.1 4.1
3 2013-05-03 2.8 2.0 3.4

我想使用的代码:

plot(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013],
type="b", col="green",
main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
xlim=c(1,30), ylim=c(0,12),
las=0)

lines(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
lines(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

points(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
points(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

结果如下图:

enter image description here

如您所见,第一个“原始”数据不是用标准点和线绘制的,尽管我使用了

type="b"

我什至尝试使用“pch”命令,它只会在我自动获得的“减号”符号上添加符号。

提问:为什么我会有这样的剧情,为什么我改不了?是因为日期定义为日期,而 R 只是假设一天中每个时间的风值吗?但是为什么它对二线和三线有效呢?

解决方法似乎可行,但我只是想知道我做错了什么。此外,我没有找到如何使用变通解决方案添加完整日期。

在此先感谢您的帮助

PS:这是我用于解决方法的代码:

plot(u10$date[month==5&year==2013],NULL,
type="p", pch="",
main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
xlim=c(1,30), ylim=c(0,12),
las=0)

lines(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013], col="green")
lines(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
lines(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

points(u10$date[month==5&year==2013],u10$u10.MF[month==5&year==2013], col="green")
points(u10$date[month==5&year==2013],u10$u10.THF[month==5&year==2013], col="red")
points(u10$date[month==5&year==2013],u10$u10.TXL[month==5&year==2013], col="blue")

最佳答案

我用最少的数据样本尝试了您的代码。当我将日期作为简单字符串提供时,我也会得到水平条或减号。如果将它们添加到 data.frame,这些字符串将变为因子格式 (str(u10$date)),如果您尝试绘制这些因子 (?plot.factor):

This functions implements a scatterplot method for factor arguments of the generic plot function. If y is missing barplot is produced. For numeric y a boxplot is used, and for a factor y a spineplot is shown. For any other type of y the next plot method is called, normally plot.default.

因此在您的情况下,R 尝试绘制箱线图(x 上的因子,y 轴上的数字)。缺点实际上是压扁的箱线图。

您可以通过以 Date 格式提供日期来解决此问题:

# Generate data
date = c(as.Date(c('2013-05-01', '2013-05-02', '2013-05-03')))
str(date) # 'Date'
u10.TXL = c(2.9,3.1,2.8)
u10.MF = c(2.0,2.1,2.0)
u10.THF = c(3.5,4.1,3.4)
u10 = data.frame(date, u10.TXL, u10.MF, u10.THF)

# Plot the lot:
plot(u10$date,u10$u10.MF,
type="b", col="green",
main="wind speed at three sites", ylab="wind speed [m/s]", xlab="date",
ylim=c(0,12),
las=0)

lines(u10$date,u10$u10.THF, col="red", type='b')
lines(u10$date,u10$u10.TXL, col="blue", type='b')

顺便说一句,你可以做'b'类型的线来同时添加点和线。

关于R 绘图 - 无法更改点(获取 "minus"标志),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23701035/

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