Alex
Alex

Reputation: 151

Using the values of a data frame, as values of the axes of a plot made with ggplot2

I'm using the ggplot2 library for the plot of a linear graph of two data.frames. Below I put the data.frames and my function. The problem is that in both the X axis and Y axis, the range of values ​​around the treaty does not appear, only 1 in 4 values. For the X axis values ​​I am using the values ​​of the power_value column data.frame DF_N_EPC_AND_FOUND_EPC. The other one I'm using data.frame to obtain a constant value. I want to see the entire range of values ​​of both the X axis and Y axis and I'm not sure how to do that.

My function:

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 data.frame:

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 data.frame:

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

enter image description here

Upvotes: 1

Views: 128

Answers (2)

Jaap
Jaap

Reputation: 83255

To add all the desired labels to your x-axis, add the following line tot your ggplot function:

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

like this:

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

which gives:

enter image description here

Implemented in your function:

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

If you want to do that for the y-axis as well, you can do the same thing with scale_y_continuous.

Upvotes: 2

Alex
Alex

Reputation: 151

almost perfect :) thank you very much, there was only a small detail. The values ​​come from a data.frame that can change so would be something like:

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

thank you very much :)

Upvotes: 0

Related Questions