如何每秒显示R ggplot2 x轴标签值? [英] How to show every second R ggplot2 x-axis label value?
问题描述
我想在演示文稿中每秒显示x轴标签列表. 下面的简化代码示例及其在图1中的输出,其中应跳过四个日期(但#2和#4).
I want to show every second of x-axis label list in the presentation. Simplified code example in the following and its output in Fig. 1 where four Dates shown but #2 and #4 should be skipped.
# https://stackoverflow.com/a/6638722/54964
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)
预期的输出:跳过第二和第四个日期.
Expected output: skip dates second and fourth.
由于rev(Vars)
逻辑,我无法将as.Date
应用于每个类别中的值的实际代码;变量molten
具有列Dates
Actual code where due to rev(Vars)
logic, I cannot apply as.Date
to the values in each category; the variable molten
has a column 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))
预期输出:跳过每个类别中的#2,#4,...值.
我以为在这里将scale_x_discrete
更改为scale_x_continuous
并在scale_x_continuous
中具有中断顺序breaks = seq(1,length(Dates),2))
,但由于以下错误而失败.
Expected output: skip #2,#4, ... values in each category.
I thought here changing scale_x_discrete
to scale_x_continuous
and having a break sequence breaks = seq(1,length(Dates),2))
in scale_x_continuous
but it fails because of the following error.
Error: `breaks` and `labels` must have the same length
基于胡安评论的提案
代码
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))
输出
Error: Discrete value supplied to continuous scale
将EricWatt的投标应用程序测试为实际代码
代码建议
Testing EricWatt's proposal application into Actual code
Code proposal
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))
输出
Error: `breaks` and `labels` must have the same length
如果具有scale_x_discrete("My dates", breaks = Dates[seq(1, length(Dates), by = 2)])
,则x轴将没有任何标签,因此为空白.
If you have scale_x_discrete("My dates", breaks = Dates[seq(1, length(Dates), by = 2)])
, you get x-axis without any labels so blank.
图. 1简化代码示例的输出, 图2 EricWatt的第一个建议的输出
Fig. 1 Output of the simplified code example, Fig. 2 Output of EricWatt's first proposal
OS:Debian 9
R:3.4.0
OS: Debian 9
R: 3.4.0
推荐答案
这适用于您的简化示例.没有您的molten
data.frame,很难根据您更复杂的绘图进行检查.
This works with your simplified example. Without your molten
data.frame it's hard to check it against your more complicated plot.
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)])
基本上,使用scale_x_date
可能会为您处理任何奇怪的数字转换日期.
Basically, use scale_x_date
which will likely handle any strange date to numeric conversions for you.
这篇关于如何每秒显示R ggplot2 x轴标签值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!