R 无法在ggplot2中正确显示x轴
提供的数据只是我正在处理的数据的一小部分。详情如下:R 无法在ggplot2中正确显示x轴,r,ggplot2,linegraph,R,Ggplot2,Linegraph,提供的数据只是我正在处理的数据的一小部分。详情如下: library(ggplot2) library(ggthemes) time <- c(0, 0.0166666666666667, 0.0333333333333333, 0.05, 0.0666666666666667, 0.0833333333333333) sent_score <- c(-8L, -1L, -49L, -26L, -8L, -18L) data = data.frame(tim
library(ggplot2)
library(ggthemes)
time <- c(0, 0.0166666666666667, 0.0333333333333333, 0.05, 0.0666666666666667,
0.0833333333333333)
sent_score <- c(-8L, -1L, -49L, -26L, -8L, -18L)
data = data.frame(time,sent_score)
ggplot(data, aes(x = time, y = sent_score)) +
geom_line() +
theme_minimal() +
ggtitle("Sentiment Scores By Time") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(seq(0, 23, 1)) +
geom_hline(yintercept = 0, color = "red", linetype = 2) +
xlab("Time")
库(ggplot2)
图书馆(主题)
时间能不能给我们一张图,让我们知道你认为x轴有什么问题?当我运行你的代码时,x轴和绘图总体上看起来很好。你可以在Ok查看图像,那么它有什么问题吗?x轴应该标记为“时间”,而不是0Ah,你是指x轴的标题。问题出在缩放x\u连续
中。您需要命名breaks
参数,否则它将被解释为name
参数,因为默认情况下name
是scale\u x\u continuous
的第一个参数。零是序列中的第一个值,因此x轴标题变为0
。代码应改为:scale\ux\u continuous(breaks=seq(0,23,1))
,可以缩短为scale\ux\u continuous(breaks=0:23)
。(如果您将代码格式化为人类可读的形式(例如,将每个ggplot语句放在新行上),则更容易发现这一点。)