【问题标题】:How to limit graph to only show points above the positive x axis?如何将图形限制为仅显示正 x 轴上方的点?
【发布时间】:2019-05-14 06:21:48
【问题描述】:

我目前的数据图如下所示:

但是,由于 2017 年左右出现负峰值,图表显示了 x 轴上方和下方的值。如何使图表仅显示 x 轴上方的值?

这是我目前用来生成图表的代码

plot(dandpw) addLegend(lty = 1)

我的数据 > head(dandpw) QLD1.Price NSW1.Price VIC1.Price SA1.Price TAS1.Price 2008-01-07 10:30:00 33.81019 36.52777 49.66935 216.45379 30.88968 2008-01-14 10:30:00 45.09321 37.55887 49.04155 248.33518 51.16057 2008-01-21 10:30:00 27.22551 29.57798 31.28935 31.56158 45.99226 2008-01-28 10:30:00 26.14283 27.32113 30.20470 31.90042 53.48170 2008-02-04 10:30:00 91.86961 36.77000 37.09027 37.57167 56.28464 2008-02-11 10:30:00 62.60607 28.83509 34.95866 35.18217 55.78961

dput(head(dandpw

【问题讨论】:

  • 你可以包含dput(head(dandpw)) 的输出吗?我们更容易粘贴到 R
  • > dput(头(dandpw))的结构(C(33.8101941747573,45.0932142857143,27.225505952381,26.1428273809524,91.8696130952381,62.6060714285714,36.5277669902913,37.558869047619,29.5779761904762,27.321130952381,36.77,28.8350892857143,49.6693527508091,49.0415476190476,31.2893452380952, 30.2047023809524,37.0902678571429,34.9586607142857,216.453786407767,248.335178571429,31.5615773809524,31.9004166666667,37.5716666666667,35.1821726190476,30.8896763754045,51.1605654761905,45.9922619047619,53.4816964285714,56.2846428571429,55.7896130952381),跨度>
  • 评论不是读取 dput 的最简单方法,而且看起来您的一些输出被截断了。您能否编辑您的问题以包含 dput 输出?
  • 是的,dput也是什么意思
  • 在 R 中输入“?dput()”并回车。

标签: r plot xts


【解决方案1】:

您可以通过两种方式做到这一点。由于没有可用的dput(只有图片),我假设您的数据在数据框中。

  1. 您可以从数据集中删除负数
  2. 您可以在图表中显示的 y 轴上设置限制(使用 ggplot2

方法 1(不推荐,因为它会改变您的数据):

#remove negatives and replace with NA. Can also replace with 0 if desired
dandpw[dandpw < 0] <- NA

方法二:

#assume dandpw is data frame
library(tidyverse)
names(dandpw)[1] <- "date" #looks like your date column might not be named
#ggplot prefers long format
dandpw <- dandpw %>% gather(variables, values, -date)
ggplot(data = dandpw, aes(x = date, y = values, color = variables)) +
 geom_line() + 
 coord_cartesian(ylim = c(0, max(dandpw$values, na.rm = T) * 1.1 ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多