如何显示每秒 R ggplot2 x 轴标签值? [英] How to show every second R ggplot2 x-axis label value?

查看:22
本文介绍了如何显示每秒 R ggplot2 x 轴标签值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在演示文稿中显示 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 并在 breaks = seq(1,length(Dates),2)) 中有一个中断序列>scale_x_continuous 但由于以下错误而失败.

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 的提案应用测试为实际代码

代码提案

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

操作系统: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆