简体   繁体   English

如何每秒显示R ggplot2 x轴标签值?

[英]How to show every second R ggplot2 x-axis label value?

I want to show every second of x-axis label list in the presentation. 我想在演示文稿中每秒显示x轴标签列表。 Simplified code example in the following and its output in Fig. 1 where four Dates shown but #2 and #4 should be skipped. 下面的简化代码示例及其在图1中的输出,其中应跳过四个日期(但#2和#4)。

# 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. 预期输出:跳过第二和第四日期。

Actual code 实际代码

Actual code where due to rev(Vars) logic, I cannot apply as.Date to the values in each category; 由于rev(Vars)逻辑,我无法将as.Date应用于每个类别中的值的实际代码; the variable molten has a column Dates 变量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)) 

Expected output: skip #2,#4, ... values in each category. 预期输出:跳过每个类别中的#2,#4,...值。 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. 我认为这里改变scale_x_discretescale_x_continuous和具有中断顺序breaks = seq(1,length(Dates),2))scale_x_continuous但它失败,因为以下错误的。

Error: `breaks` and `labels` must have the same length 

Proposal based Juan's comments 基于提案的Juan的评论

Code

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)) 

Output 输出量

Error: Discrete value supplied to continuous scale

Testing EricWatt's proposal application into Actual code 将EricWatt的投标应用程序测试为实际代码

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))  

Output 输出量

Error: `breaks` and `labels` must have the same length 

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. 如果具有scale_x_discrete("My dates", breaks = Dates[seq(1, length(Dates), by = 2)]) ,则x轴将没有任何标签,因此为空白。

Fig. 1 Output of the simplified code example, Fig. 2 Output of EricWatt's first proposal 图1简化代码示例的输出,图2 EricWatt的第一个建议的输出

在此处输入图片说明 在此处输入图片说明

OS: Debian 9 操作系统:Debian 9
R: 3.4.0 R:3.4.0

This works with your simplified example. 这适用于您的简化示例。 Without your molten data.frame it's hard to check it against your more complicated plot. 没有您的molten data.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)])

Basically, use scale_x_date which will likely handle any strange date to numeric conversions for you. 基本上,使用scale_x_date可能会为您处理任何奇怪的数字转换日期。

My solution eventually on the actual code motivated by the other linked thread and EricWatt's answer 我的解决方案最终是由其他链接线程和EricWatt的答案所激发的实际代码

# Test data of actual data here # https://stackoverflow.com/q/45130082/54964

ggplot(data = molten, aes(x = as.Date(Time.data, format = "%d.%m.%Y"), y = value)) + 
  geom_bar(aes(fill = variable), stat = "identity", position = "dodge") +
  facet_wrap( ~ variable, scales="free") +
  theme_bw() + # has to be before axis text manipulations because disables their effect otherwise
  theme(axis.text.x = element_text(angle = 90, hjust=1), 
        text = element_text(size=10)) +
  scale_x_date(date_breaks = "2 days", date_labels = "%d.%m.%Y")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM