如何使用ggplot2而不是R中的默认绘图来绘制三条点线
我有三个矩阵,我想用ggplot2绘制这个图。我有下面的数据如何使用ggplot2而不是R中的默认绘图来绘制三条点线,r,ggplot2,points,R,Ggplot2,Points,我有三个矩阵,我想用ggplot2绘制这个图。我有下面的数据 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
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()
库(集群)
需要(ggplot2)
需要(天平)
要求(2)
数据(俄语)
我不太确定你在找什么,我猜一下
你的数据
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)
这就是你的意思吗
如果您想用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()
#ggplot像一个长表,而不是一个宽表,因此重新塑造数据,并显式添加“time”变量(即my_time=1:10)
要求(2)
df我不太确定你想要什么,这里是一个猜测
你的数据
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)
这就是你的意思吗
如果您想用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()
#ggplot像一个长表,而不是一个宽表,因此重新塑造数据,并显式添加“time”变量(即my_time=1:10)
要求(2)
df我不太确定你想要什么,这里是一个猜测
你的数据
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)
这就是你的意思吗
如果您想用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()
#ggplot像一个长表,而不是一个宽表,因此重新塑造数据,并显式添加“time”变量(即my_time=1:10)
要求(2)
df我不太确定你想要什么,这里是一个猜测
你的数据
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)
这就是你的意思吗
如果您想用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()
#ggplot像一个长表,而不是一个宽表,因此重新塑造数据,并显式添加“time”变量(即my_time=1:10)
要求(2)
我只是按照你的代码更新了我的代码。但是还是不行。你能看一下吗?当然,我已经更新了我的答案。似乎您的新对象max\u Wmk
、min\u Wmk
和w
不是像max
、min
和w
那样的向量,因此使data.frame
以同样的方式工作。我添加了几行代码将它们强制为向量,现在它可以按预期工作了。我只是按照您的代码进行了更新。但是还是不行。你能看一下吗?当然,我已经更新了我的答案。似乎您的新对象max\u Wmk
、min\u Wmk
和w
不是像max
、min
和w
那样的向量,因此使data.frame
以同样的方式工作。我添加了几行代码将它们强制为向量,现在它可以按预期工作了。我只是按照您的代码进行了更新。但是还是不行。你能看一下吗?当然,我已经更新了我的答案。似乎您的新对象max\u Wmk
、min\u Wmk
和w
不是像max
、min
和w
那样的向量,因此使data.frame
以同样的方式工作。我添加了几行代码将它们强制为向量,现在它可以按预期工作了。我只是按照您的代码进行了更新。但是还是不行。你能看一下吗?当然,我已经更新了我的答案。似乎您的新对象max\u Wmk
、min\u Wmk
和w
不是像max
、min
和w
那样的向量,因此使data.frame
以同样的方式工作。我已经添加了几行代码来将它们强制为向量,现在它可以正常工作了。