设置R ggplot2的x轴
如何使x轴显示“xaxisTitles”向量中的文本 以下是您可以运行的我的代码:设置R ggplot2的x轴,r,ggplot2,R,Ggplot2,如何使x轴显示“xaxisTitles”向量中的文本 以下是您可以运行的我的代码: require(ggplot2) require(reshape) xaxisTitles<- cbind("a","b","c","d","e","f","g","h","j","k") df <- data.frame(time = 1:10, a = cumsum(rnorm(10)), b = cumsum(rnorm(10)),
require(ggplot2)
require(reshape)
xaxisTitles<- cbind("a","b","c","d","e","f","g","h","j","k")
df <- data.frame(time = 1:10,
a = cumsum(rnorm(10)),
b = cumsum(rnorm(10)),
c = cumsum(rnorm(10)))
df <- melt(df , id = 'time', variable_name = 'series')
# plot on same grid, each series colored differently --
# good if the series have same scale
ggplot(df, aes(time,value)) + geom_line(aes(colour = series))+ theme(axis.text.x = xaxisTitles)
最好的方法是将
时间
变量设置为一个因子,而不是一个数字向量,只要您记得调整组
:
df$time = factor(xaxisTitles[df$time])
ggplot(df, aes(time, value)) + geom_line(aes(colour = series, group=series))
(如果您不添加
group=series
参数,它将不知道您想要在x
轴上连接跨越因子的线)。最好的方法是将时间
变量设为因子而不是数字向量,只要您记得调整组
:
df$time = factor(xaxisTitles[df$time])
ggplot(df, aes(time, value)) + geom_line(aes(colour = series, group=series))
(如果不添加
group=series
参数,它将不知道您要在x
轴上的系数上连接线)。您可以使用scale\u x\u discrete设置标签
ggplot(df, aes(time,value)) + geom_line(aes(colour = series))+ scale_x_discrete(labels= xaxisTitles)
这里有一些更有用的信息您可以使用scale\u x\u离散设置标签
ggplot(df, aes(time,value)) + geom_line(aes(colour = series))+ scale_x_discrete(labels= xaxisTitles)
这里有一些更有用的信息出现错误的原因是
主题(…)
用于设置轴文本的外观(例如,颜色、字体系列、字体、大小、方向等),而不是文本的值。要做到这一点,正如@SteveReno所指出的,您必须使用scale\ux\udiscrete(…)
require(ggplot2)
需要(重塑)
种子集(321)
#xaxisTitles出现错误的原因是主题(…)
用于设置轴文本的外观(例如,颜色、字体系列、字体、大小、方向等),而不是文本的值。要做到这一点,正如@SteveReno所指出的,您必须使用scale\ux\udiscrete(…)
require(ggplot2)
需要(重塑)
种子集(321)
#xaxisTitles