Léo*_* 준영 6 debian r ggplot2 axis-labels
我想在演示文稿中每秒显示x轴标签列表。下面的简化代码示例及其在图1中的输出,其中应跳过四个日期(但#2和#4)。
# /sf/answers/464710571/
require(ggplot2)
my.dates = as.Date(c("2011-07-22","2011-07-23",
"2011-07-24","2011-07-28","2011-07-29"))
my.vals = c(5,6,8,7,3)
my.data <- data.frame(date =my.dates, vals = my.vals)
plot(my.dates, my.vals)
p <- ggplot(data = my.data, aes(date,vals))+ geom_line(size = 1.5)
Run Code Online (Sandbox Code Playgroud)
预期输出:跳过第二和第四日期。
由于rev(Vars)逻辑原因,我无法将实际代码应用于as.Date每个类别中的值;变量molten有一个列Dates
p <- ggplot(molten, aes(x = rev(Vars), y = value)) +
geom_bar(aes(fill=variable), stat = "identity", position="dodge") +
facet_wrap( ~ variable, scales="free") +
scale_x_discrete("Column name dates", labels = rev(Dates))
Run Code Online (Sandbox Code Playgroud)
预期输出:跳过每个类别中的#2,#4,...值。我以为这里要更改scale_x_discrete为scale_x_continuous中断顺序breaks = seq(1,length(Dates),2)),scale_x_continuous但由于以下错误而失败。
Error: `breaks` and `labels` must have the same length
Run Code Online (Sandbox Code Playgroud)
码
ggplot(data = my.data, aes(as.numeric(date), vals)) +
geom_line(size = 1.5) +
scale_x_continuous(breaks = pretty(as.numeric(rev(my.data$date)), n = 5))
Run Code Online (Sandbox Code Playgroud)
输出量
Error: Discrete value supplied to continuous scale
Run Code Online (Sandbox Code Playgroud)
代码提案
p <- ggplot(molten, aes(x = rev(Vars), y = value)) +
geom_bar(aes(fill=variable), stat = "identity", position="dodge") +
facet_wrap( ~ variable, scales="free") +
scale_x_discrete("My dates", breaks = Dates[seq(1, length(Dates), by = 2)], labels = rev(Dates))
Run Code Online (Sandbox Code Playgroud)
输出量
Error: `breaks` and `labels` must have the same length
Run Code Online (Sandbox Code Playgroud)
如果有scale_x_discrete("My dates", breaks = Dates[seq(1, length(Dates), by = 2)]),则x轴将没有任何标签,因此为空白。
图1简化代码示例的输出,图2 EricWatt的第一个建议的输出
操作系统:Debian 9
R:3.4.0
这适用于您的简化示例。没有你的moltendata.frame 很难对照你更复杂的情节来检查它。
ggplot(data = my.data, aes(date, vals)) +
geom_line(size = 1.5) +
scale_x_date(breaks = my.data$date[seq(1, length(my.data$date), by = 2)])
Run Code Online (Sandbox Code Playgroud)
基本上,使用scale_x_datewhich 可能会为您处理任何奇怪的日期到数字的转换。
| 归档时间: |
|
| 查看次数: |
7496 次 |
| 最近记录: |