Home > database >  Access data with ggplot
Access data with ggplot

Time:10-14

I have below

library(ggplot2)
library(dplyr)
ggplot(head(economics_long, 100) %>% mutate(value01 = value01 * 2))  
  geom_line(aes(date, value01, colour = variable))  
  geom_line(data = .data, aes(date, value01), colour = 'blue')

My goal is to access the data information in a different layer. This is a simple example, but I am looking for some way if it is possible to access the data object in different layer, as above code is generating error.

Thanks for your help

CodePudding user response:

The solution is simple.

ggplot(economics_long)   
  geom_line(aes(date, value01, colour = variable)) 

should give you the plot you want.

Due to your head() operation, you limited the data set to data points that only have the pce value for the variable column.

library(ggplot2)
library(dplyr)
library(data.table)
sample <- head(economics_long, 100) %>% mutate(value01 = value01 * 2)
sample[,.N, by=.(variable)]
" variable   N
1:      pce 100"

That was the reason why you couldn't see additional 'layers'.

If you simply use the whole data set with code lines I wrote at the start of this answer you should get the plot you want.

I hope this helped

[EDIT: Of course you should double the value01 before plotting the graph if that is the value you want to show in your graph.]

[EDIT2: previous answer was an answer based on the assumption that the person who posted this wanted to see different lines in terms of the variable column. However after re-reading the question it might be the case that the person wants to know how to add an additional graph to the original plot. If that's the case, it can be achieved by

library(ggplot2)
library(dplyr)
library(data.table)

sample <- head(economics_long, 100) %>% mutate(value01 = value01 * 2) %>% 
  as.data.table()
sample <- sample %>% 
  mutate(value02 = -value01)

ggplot(sample)  
  geom_line(aes(date, value01, colour = variable))  
  geom_line(aes(date, value02), colour = "blue")   
  ylab("Value")

If the data source is different, one should use the approach:

plot <- ggplot()   
  geom_line(data = data1, aes(date, value01, colour = variable))  
  geom_line(data = data2, aes(date, value01), colour = "blue")

data1 and data2 should be replaced accordingly.

CodePudding user response:

If I understand you correctly, you want to be able to access the data that was initally passed to the ggplot call to further manipulate it in specific layers. (Of course, there is no point in having unchanged data passed to the data parameter, since the layer will by default inherit the data passed to the initial ggplot call.)

It is possible to indirectly access the plot data by passing a function to the data argument of a geom layer. This will be recognised as a request to process the layer data with the given function prior to the layer using it. Even better, if you use a dplyr pipe starting with the . placeholder, this will be interpreted as a functional sequence, which is recognised as a function by the data argument.

The upshot is that you can access the data by using the . placeholder, as long as you then have a dplyr-style pipe afterwards. In the simplest case, you could add a useless pipe command like data = . %>% {}, which of course does nothing to the data, and is therefore the same as leaving the data argument out altogether, but at least demonstrates that this works and doesn't cause an error (note the blue line is plotted over the red line)

ggplot(head(economics_long, 100) %>% mutate(value01 = value01 * 2))  
  geom_line(aes(date, value01, colour = variable))  
  geom_line(data = . %>% {}, aes(date, value01), colour = 'blue')

Where this feature comes in very handy is in altering variables in the data passed to ggplot that only affect a single layer. For example, if we wanted to half the value01 variable in the second geom_line, we could do:

ggplot(head(economics_long, 100) %>% mutate(value01 = value01 * 2))  
  geom_line(aes(date, value01, colour = variable))  
  geom_line(data = . %>% mutate(value01 = 0.5 * value01), 
            aes(date, value01, color = 'pce cut in half'))

  • Related