简体   繁体   English

如何使用ggplot2而不是R中的默认图绘制三点线

[英]How to plot three point lines using ggplot2 instead of the default plot in R

I have three matrix and I want to plot the graph using ggplot2. 我有三个矩阵,我想使用ggplot2绘制图形。 I have the data below. 我有下面的数据。

library(cluster)
require(ggplot2)
require(scales)
require(reshape2)

data(ruspini)
x <- as.matrix(ruspini[-1])


w <- matrix(W[4,])

df <- melt(data.frame(max_Wmk, min_Wmk, w, my_time = 1:10), id.var = 'my_time')

ggplot(df, aes(colour = variable, x = my_time, y = value)) +
  geom_point(size = 3) +
  geom_line() +
  scale_y_continuous(labels = comma) +
  theme_minimal()

I want to add the three plots into one plot using a beautiful ggplot2. 我想使用漂亮的ggplot2将这三个图添加到一个图中。 Moreover, I want to make the points with different values have different colors. 此外,我想使具有不同值的点具有不同的颜色。

I'm not quite sure what you're after, here's a guess 我不太确定你在追求什么,这是一个猜测

Your data... 您的数据...

max <- c(175523.9, 33026.97, 21823.36, 12607.78, 9577.648, 9474.148, 4553.296, 3876.221, 2646.405, 2295.504)
min <- c(175523.9, 33026.97, 13098.45, 5246.146, 3251.847, 2282.869, 1695.64, 1204.969, 852.1595, 653.7845)
w <- c(175523.947, 33026.971, 21823.364, 5246.146, 3354.839, 2767.610, 2748.689,   1593.822, 1101.469, 1850.013)

Slight modification to your base plot code to make it work... 稍微修改您的基本绘图代码以使其工作...

plot(1:10,max,type='b',xlab='Number',ylab='groups',col=3)
points(1:10,min,type='b', col=2)
points(1:10,w,type='b',col=1)

Is this what you meant? 这是你的意思吗?

在此处输入图片说明

If you want to reproduce this with ggplot2 , you might do something like this... 如果要使用ggplot2重现此ggplot2 ,则可以执行以下操作...

# ggplot likes a long table, rather than a wide one, so reshape the data, and add the 'time' variable explicitly (ie. my_time = 1:10)
require(reshape2)
df <- melt(data.frame(max, min, w, my_time = 1:10), id.var = 'my_time')

# now plot, with some minor customisations...
require(ggplot2); require(scales)
ggplot(df, aes(colour = variable, x = my_time, y = value)) +
  geom_point(size = 3) +
  geom_line() +
  scale_y_continuous(labels = comma) +
  theme_minimal()

在此处输入图片说明

UPDATE after the question was edited and the example data changed, here's an edit to suit the new example data: 编辑问题并更改示例数据后进行UPDATE ,这是适合新示例数据的编辑:

Here's your example data (there's scope for simplification and speed gains here, but that's another question): 这是您的示例数据(此处存在简化和提高速度的范围,但这是另一个问题):

library(cluster)
require(ggplot2)
require(scales)
require(reshape2)

data(ruspini)
x <- as.matrix(ruspini[-1])

wss <- NULL
W=matrix(data=NA,ncol=10,nrow=100)

for(j in 1:100){
  k=10  
  for(i in 1: k){
    wss[i]=kmeans(x,i)$tot.withinss    
  }
  W[j,]=as.matrix(wss)
}

max_Wmk <- matrix(data=NA, nrow=1,ncol=10)

for(i in 1:10){
  max_Wmk[,i]=max(W[,i],na.rm=TRUE)
}

min_Wmk <- matrix(data=NA, nrow=1,ncol=10)
for(i in 1:10){
  min_Wmk[,i]=min(W[,i],na.rm=TRUE)
}

w <- matrix(W[4,])

Here's what you need to do to make the three objects into vectors so you can make the data frame as expected: 这是将三个对象变成向量的操作,以便可以按预期制作数据帧:

max_Wmk <- as.numeric(max_Wmk)
min_Wmk <- as.numeric(min_Wmk)
w <- as.numeric(w)

Now reshape and plot as before... 现在像以前一样重塑和绘图...

df <- melt(data.frame(max_Wmk, min_Wmk, w, my_time = 1:10), id.var = 'my_time')

ggplot(df, aes(colour = variable, x = my_time, y = value)) +
  geom_point(size = 3) +
  geom_line() +
  scale_y_continuous(labels = comma) +
  theme_minimal()

And here's the result: 结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM