【问题标题】:Using the values of a data frame, as values of the axes of a plot made with ggplot2使用数据框的值,作为用 ggplot2 绘制的图的轴值
【发布时间】:2014-05-22 13:28:55
【问题描述】:

我使用ggplot2 库绘制两个data.frames 的线性图。下面我放了 data.frames 和我的函数。问题是在X轴和Y轴上,条约周围的数值范围都没有出现,只有4个数值中的1个。对于X轴值我使用的是power_value列data.frameDF_N_EPC_AND_FOUND_EPC的值。另一个我正在使用 data.frame 来获得一个常数值。我想查看 X 轴和 Y 轴的整个取值范围,我不知道该怎么做。

我的功能:

LINER_GRAPH_POWER_LIST_VALUES<-function(DF_N_EPC_AND_FOUND_EPC, DF_READ_EXTERNAL_LIST_EPC_TAGS ){
  require(reshape2)
  df.m <- melt(DF_N_EPC_AND_FOUND_EPC, id=c("power_value"), measure=c("total_epc","found_epc"), variable.name="epc")
  require(ggplot2)
  ggplot(df.m, aes(x=power_value, y=value, color=epc)) +
    geom_line() +
    geom_point() +
    geom_hline(yintercept=nrow(DF_READ_EXTERNAL_LIST_EPC_TAGS), color="green")
}

DF_N_EPC_AND_FOUND_EPC 数据帧:

power_value total_epc found_epc
1         31.0        11         5
2         30.5        12         5
3         30.0        11         5
4         29.5        11         5
5         29.0        10         5
6         28.5        11         5
7         28.0        11         5
8         27.5        10         4
9         27.0        10         4
10        26.5        10         4
11        26.0        10         4
12        25.5        10         3
13        25.0         9         3

DF_READ_EXTERNAL_LIST_EPC_TAGS 数据帧:

TAGS_IDE
1 00000000000000000000A288
2 000000000000000CCC002001
3 00000000000000000000A194
4 00000000000000000000A284
5 00000000000000000000A332
6 00000000000000000000A002
7 00000000000000000000A262
8 00000000000000000000A261

【问题讨论】:

  • 您能更好地解释“条约周围的值范围没有出现,只有四分之一的值”部分吗?从我所看到的情况来看,所有的值都被绘制出来了。
  • 对不起,我的意思是轴的标签。我想查看所有值​​​,即在图表的情况下将是“25、25.5、26、26.5 31 ....”,不仅是26、28和30

标签: r ggplot2 dataframe date-range


【解决方案1】:

要将所有需要的标签添加到您的 x-axis,请将以下行添加到您的 ggplot 函数中:

scale_x_continuous(breaks = c(..add the desired breaks here..))

像这样:

ggplot(df.m, aes(x = power_value, y = value, color = epc)) +
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = c(25.0,25.5,26.0,26.5,27.0,27.5,28.0,28.5,29.0,29.5,30.0,30.5,31.0)) +
  geom_hline(yintercept = nrow(DF_READ_EXTERNAL_LIST_EPC_TAGS), color = "green")

给出:

在你的函数中实现:

LINER_GRAPH_POWER_LIST_VALUES <- function(DF_N_EPC_AND_FOUND_EPC, DF_READ_EXTERNAL_LIST_EPC_TAGS ){
  require(reshape2)
  df.m <- melt(DF_N_EPC_AND_FOUND_EPC, id = c("power_value"), measure = c("total_epc","found_epc"), variable.name = "epc")
  require(ggplot2)
  ggplot(df.m, aes(x = power_value, y = value, color = epc)) +
    geom_line() +
    geom_point() +
    scale_x_continuous(breaks = c(df.m$power_value)) +
    geom_hline(yintercept = nrow(DF_READ_EXTERNAL_LIST_EPC_TAGS), color="green")
}

如果您也想为y-axis 做同样的事情,您可以用scale_y_continuous 做同样的事情。

【讨论】:

    【解决方案2】:

    几乎完美:) 非常感谢,只有一个小细节。值来自可以更改的data.frame,如下所示:

    LINER_GRAPH_POWER_LIST_VALUES<-function(DF_N_EPC_AND_FOUND_EPC, DF_READ_EXTERNAL_LIST_EPC_TAGS ){
      require(reshape2)
      df.m <- melt(DF_N_EPC_AND_FOUND_EPC, id=c("power_value"), measure=c("total_epc","found_epc"), variable.name="epc")
      require(ggplot2)
      ggplot(df.m, aes(x=power_value, y=value, color=epc)) +
        geom_line() +
        geom_point() +
        scale_x_continuous(breaks=c(DF_N_EPC_AND_FOUND_EPC$power_value)) +
        geom_hline(yintercept=nrow(DF_READ_EXTERNAL_LIST_EPC_TAGS), color="green")
    }
    

    非常感谢:)

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      相关资源
      最近更新 更多