Change the labels of the axes when building a digital vector with the coefficient "as.numeric"

I unsuccessfully searched for a way to hold axes, designated in a certain way, after creating a linear model graph.

My details

    Sample                  ActB
    Pre-Amp_Ctrl cDNA       5.607907144
    Pre-Amp_Ctrl cDNA 10e-1 8.916634343
    Pre-Amp_Ctrl cDNA 10e-2 12.6501345
    Pre-Amp_Ctrl cDNA 10e-3 16.32385192
    Pre-Amp_Ctrl cDNA 10e-4 20.30327678
    Pre-Amp_Ctrl cDNA 10e-5 23.40471201

When i do it ok

    ggplot(NP, aes(x=NP$Sample, y=NP$actbct)) +
    geom_point()

I get this:

enter image description here

where samples are labeled with ticks.

However, when I try to add a trend line, I need to change the factor vector to "as.numeric" and I will lose the marked ticks.

    ggplot(NP, aes(x=as.numeric(NP$Sample), y=NP$actbct)) +
    geom_point() +
    geom_smooth(method= "lm", color = "red")

and get this image:

enter image description here

Is there any possible way to keep the patterns tagged as well as have a trend line? I know I can do it in Excel, but it would be great to do it in R too.

+4
source share
2 answers

scale_x_continuous . , .

NP <- read.csv("tmp", header = T)

library(ggplot2)
ggplot(NP, aes(as.numeric(Sample), ActB)) +
    geom_point() +
    geom_smooth(method = "lm", color = "red") +
    scale_x_continuous(breaks = 1:nrow(NP), labels = NP$Sample) +
    labs(x = "Sample") +
    theme(axis.text.x = element_text(angle = 70, hjust = 1))

enter image description here

+3
NP <- read.table(text='
Sample ActB
"Pre-Amp_Ctrl cDNA" 5.607907144
"Pre-Amp_Ctrl cDNA 10e-1" 8.916634343
"Pre-Amp_Ctrl cDNA 10e-2" 12.6501345
"Pre-Amp_Ctrl cDNA 10e-3" 16.32385192
"Pre-Amp_Ctrl cDNA 10e-4" 20.30327678
"Pre-Amp_Ctrl cDNA 10e-5" 23.40471201
', header=T)

library(ggplot2)
   ggplot(NP, aes(x=NP$Sample, y=NP$ActB)) +
    geom_point() +
    geom_smooth(aes(x=as.numeric(NP$Sample), y=NP$ActB), method= "lm", color = "red")

enter image description here

+1

Source: https://habr.com/ru/post/1686491/


All Articles