ggplot()没有预期的数字

时间:2018-10-31 09:50:09

标签: r ggplot2

我有一个数据框StatMNS StatMNS我在不同区域(x)上绘制每种MNS(颜色)的统计数据(y)

ggplot(statMNS,aes(x=1:n))+
geom_point(aes(y=as.numeric(statMNS$v1qu_lidar)),size=2,col="skyblue")+
geom_point(aes(y=as.numeric(statMNS$v1qu_photo)),size=2,col="tan")+
geom_point(aes(y=as.numeric(statMNS$v3qu_lidar)),size=2,col="slateblue")+
geom_point(aes(y=as.numeric(statMNS$v3qu_photo)),size=2,col="tan3")+
geom_point(aes(y=as.numeric(statMNS$vmax_lidar)),size=2,col="navyblue")+
geom_point(aes(y=as.numeric(statMNS$vmax_photo)),size=2,col="tan4")+
geom_point(aes(y=as.numeric(statMNS$vmean_lidar)),size=2,col="skyblue4")+
geom_point(aes(y=as.numeric(statMNS$vmean_photo)),size=2,col="tan2")+
...+
labs(title = "Stat placette\n", x = "Placette", y = "Hauteur (m)", color = "Legend Title\n") +
scale_color_manual(values=c("max lidar"="navyblue", "max photo"="tan4",
                          "3e qu. lidar"="slatebule","3e qu. phot"="tan3",
                          "moy lidar"="skyblue4", "moy photo"="tan2",
                          "mediane lidar"="skyblue3","mediane photo"="tan1",
                          "1r qu. lidar"="skyblue","1r qu. photo"="tan",
                          "min lidar"="powderblue", "min photo"="lightgoldenrod1") )


statMNS2 <- gather(data = statMNS,key = TYPE,value = VAL,vmax_lidar,vmax_photo,v3qu_lidar,v3qu_photo,vmean_lidar,vmean_photo,vmedian_lidar,vmedian_photo,v1qu_lidar,v1qu_photo,vmin_lidar,vmin_photo)

ggplot(statMNS2,aes(statMNS2$readerName,VAL),color=TYPE)+geom_point()`

首先,我想展示传说 其次,我想将x向量1:n替换为区域名称(dataFrame的column readerName)

我尝试在StatMNS2中重新排列我的数据
StatMNS2
但是我有错误(离散值提供给连续刻度)

dput(statMNS)
structure(list(readerName = structure(list(X1 = "LBO_1", X2 = "LBO_11", 
X3 = "LBO_12", X4 = "LBO_13", X5 = "LBO_2", X6 = "LBO_3", 
X7 = "LBO_4", X8 = "LBO_5", X9 = "LBO_6", X10 = "LBO_7"), .Names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10")), 
vmax_lidar = structure(list(X1 = 167.4, X2 = 159.1, X3 = 163.5, X4 = 163.4, 
X5 = 160, X6 = 158.9, X7 = 153.8, X8 = 151, X9 = 157.3, X10 = 155.8), .Names 
= c("X1","X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10")), 
vmax_photo = structure(list(X1 = 148.8, X2 = 157.5, X3 = 161.1, X4 = 161.7, 
X5 = 158.1, X6 = 150.8, X7 = 141.6, X8 = 147.1, X9 = 155.1, X10 = 151.4), 
.Names = c("X1","X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10")), 
v3qu_lidar = ...
, .Names = 
c("readerName", 
"vmax_lidar", "vmax_photo", "v3qu_lidar", "v3qu_photo", "vmean_lidar", 
"vmean_photo", "vmedian_lidar", "vmedian_photo", "v1qu_lidar", 
"v1qu_photo", "vmin_lidar", "vmin_photo"), row.names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10"), class = 
"data.frame")


dput(statMNS2)
structure(list(readerName = list("LBO_1", "LBO_11", "LBO_12", 
"LBO_13", "LBO_2", "LBO_3", "LBO_4", "LBO_5", "LBO_6", "LBO_7", 
"LBO_1", "LBO_11", "LBO_12", "LBO_13", "LBO_2", "LBO_3", 
"LBO_4", "LBO_5", "LBO_6", "LBO_7", "LBO_1", "LBO_11", "LBO_12", 
"LBO_13", "LBO_2", "LBO_3", "LBO_4", "LBO_5", "LBO_6", "LBO_7", 
"LBO_1", "LBO_11", "LBO_12", "LBO_13", "LBO_2", "LBO_3", 
"LBO_4", "LBO_5", ...,
TYPE = c("vmax_lidar", ..., "vmax_photo", "v3qu_lidar", 
..., "v3qu_photo", "vmean_lidar", ..., "vmean_photo", "vmean_photo", ..., 
"vmedian_lidar", ..., "vmedian_photo", "v1qu_lidar",..."v1qu_photo", 
"vmin_lidar", "vmin_photo", "vmin_photo", "vmin_photo", "vmin_photo", 
"vmin_photo", "vmin_photo", "vmin_photo", "vmin_photo"), VAL = list(
167.4, 159.1, 163.5, 163.4, 160, 158.9, 153.8, 151, 157.3, 
155.8, 148.8, 157.5, 161.1, 161.7, 158.1, 150.8, 141.6, 147.1, 
155.1, 151.4, 146.6, 153.3, 160.8, 158.7, 156.3, 146.6, 140.6, 
142.6, 151.2, 152.7, 146.3, 151.5, 159.4, 157.3, 155, 145.2, 
140.2, 142.3, 149.3, 149.7, 147.8, 150.4, 160, 155.4, 154.1, 
145.2, 140.8, 142.9, 150.1, 151.4, 146.2, 148.9, 158.8, 154.2, 
153.2, 143.3, 140.2, 142.3, 149.2, 149.5, 146.3, 150.9, 160.1, 
156.3, 154.8, 144.4, 140.4, 142.4, 149, 151.6, 146.1, 147.4, 
158.9, 153.4, 153.7, 143.5, 140.2, 142.3, 148.8, 149.5, 146.2, 
146.3, 159.3, 151.1, 151.8, 142.6, 140.3, 142.3, 148.8, 150.1, 
146, 146.4, 158.3, 151.2, 150.8, 141, 140, 142.2, 148.6, 
149.1, 145.8, 145.6, 149.5, 149.6, 149.5, 138.8, 140, 141.9, 
148.5, 148.3, 145.1, 145.1, 155.4, 149.8, 149.1, 138, 139.1, 
141.3, 148.4, 148.4)), row.names = c(NA, -120L), .Names = c("readerName", 
"TYPE", "VAL"), class = "data.frame")

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

由于您没有提供示例数据,因此很难重现所需的内容。但是,我想您的问题出在屏幕快照的最后一行,因为您可以将color保留在aes()内,并且只需要readerName而不是statMNS2$readerName

我使用了一些示例数据来使其工作,并为您提供了一个可能适用于您的数据的示例。

library(ggplot2)

# sample data
ggplot(ToothGrowth, aes(x=supp, y=len, color=dose))+
  geom_point(size=2)

# try on your data
statMNS2$VAL <- as.numeric(statMNS2$VAL)
ggplot(statMNS2, aes(x=readerName, y=VAL, color=TYPE))+
  geom_point(size=2)

它能给您预期的结果吗?

编辑: 使用短版数据时,一切正常。这个例子对你有用吗?开始一个新的R-Session,复制并粘贴下面的整个代码,看看它是否有效。

# short version of your data
statMNS2 <- data.frame(readerName = c("LBO_1", "LBO_11", "LBO_12", 
                          "LBO_13", "LBO_2", "LBO_3", "LBO_4", "LBO_5", "LBO_6", "LBO_7", 
                          "LBO_1", "LBO_11", "LBO_12", "LBO_13", "LBO_2", "LBO_3", 
                          "LBO_4", "LBO_5", "LBO_6", "LBO_7"),
           TYPE = c("vmax_lidar", "vmax_photo", "v3qu_lidar", 
                    "v3qu_photo", "vmean_lidar", "vmean_photo", "vmean_photo",  
                    "vmedian_lidar", "vmedian_photo", "v1qu_lidar","v1qu_photo", 
                    "vmin_lidar", "vmin_photo", "vmin_photo", "vmin_photo", "vmin_photo", 
                    "vmin_photo", "vmin_photo", "vmin_photo", "vmin_photo"), 
           VAL = c(
             167.4, 159.1, 163.5, 163.4, 160, 158.9, 153.8, 151, 157.3, 
             155.8, 148.8, 157.5, 161.1, 161.7, 158.1, 150.8, 141.6, 147.1, 
             155.1, 151.4))

# same ggplot call as above
library(ggplot2)
ggplot(statMNS2, aes(x=readerName, y=VAL, color=TYPE))+
  geom_point(size=2)