littleworth
littleworth

Reputation: 5169

How to show the label in X-axis based on input column

I have the following data frame:

library(tidyverse)
plot_dat <- structure(list(Qubit_Conc = c(45.6, 16.3, 27.7, 43, 77.7, 126, 
186), unit = c("ng/uL", "ng/uL", "ng/uL", "ng/uL", "ng/uL", "ng/uL", 
"ng/uL"), Fluoresence = c(4422.89, 648.5, 1648.47, 3932.18, 12444.22, 
27644.98, 44428.57), node = c("0.1%FBS", "Free", "5uM", "10uM", 
"20uM", "40uM", "80uM")), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"))

It looks like this:

> plot_dat
# A tibble: 7 × 4
  Qubit_Conc unit  Fluoresence node   
       <dbl> <chr>       <dbl> <chr>  
1       45.6 ng/uL       4423. 0.1%FBS
2       16.3 ng/uL        648. Free   
3       27.7 ng/uL       1648. 5uM    
4       43   ng/uL       3932. 10uM   
5       77.7 ng/uL      12444. 20uM   
6      126   ng/uL      27645. 40uM   
7      186   ng/uL      44429. 80uM   

What I want to do is show the x-axis value based on Qubit_Conc column. I tried this but doesn't work:

ggplot(plot_dat, aes(x = Qubit_Conc, y = Fluoresence)) +
  geom_line() +
  geom_point(size = 4) +
  theme_bw() + 
  scale_x_continuous( labels = as.character(Qubit_Conc), breaks = Qubit_Conc)  

Especially, the line scale_x_continuous() suppose to determine the x-axis:

The error I get is this:

Error in check_breaks_labels(breaks, labels) : 
  object 'Qubit_Conc' not found

The default fig without scale_x_continuous()

enter image description here

The red arrow are the point in x-axis we want to show. So instead of 50, 100, 150, the x-axis should show: 16.3, 27.7, 43.0, 45.6, 77.7, 126.0, 186.0.

What's the right way to do it?

Upvotes: 0

Views: 255

Answers (1)

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

You can pass the column as a variable plot_dat$Qubit_Conc, because the ggplot doesn't seem to understand the column name reference

ggplot(plot_dat, aes(x = Qubit_Conc, y = Fluoresence)) +
  geom_line() +
  geom_point(size = 4) +
  theme_bw() + 
  scale_x_continuous(breaks=plot_dat$Qubit_Conc,
                     minor_breaks=NULL)

I removed the minor breaks because they look weird with those uneven intervals, but you can change ir back if you want.

enter image description here

Upvotes: 1

Related Questions