user20466352
user20466352

Reputation:

R plot x and y axes changes

I have my data:

x <- list(10,50,100,250,500,2500)
y <- list(0.09090909,0.01960784,0.003984064,0.003984064,0.001996008,0.0003998401)

I want to link my data to code found on this website

log_c <- seq(-12, 0, by = 0.1)
df <- data.frame(log_c = log_c, response = V(log_c, E, HS, log_EC50))

ggplot(df, aes(log_c, response)) + 
  geom_line()

enter image description here

I want to put the x data in place of log_c and y data in place of response. These are my beginnings in R, so I am asking for help

Upvotes: 1

Views: 135

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226087

At a minimum this will work:

  1. you have expressed your x and y values as lists; it will work better if they are (atomic) vectors (which you would have gotten using c() instead of list()). Put them into a data frame (not strictly necessary, but best practice):
df <- data.frame(x=unlist(x), y = unlist(y))
  1. Use the data frame to draw your plot:
library(ggplot2)
ggplot(df, aes(x, y)) + geom_line()

To get your graph looking a little more like the example in your post,

scalefun <- function(y) (y-min(y))/diff(range(y))
ggplot(df, aes(x, y = scalefun(y))) + geom_line() + scale_x_log10()

Upvotes: 5

TarJae
TarJae

Reputation: 78917

Same logic as @Ben Bolker: Just another way:

library(tidyverse)

tibble(x,y) %>% 
  unnest() %>% 
  ggplot(aes(x, y))+
  geom_line()

enter image description here

Upvotes: 1

Related Questions