How to delete letter "X" from numerical values in ggplot x-axis
1
0
Entering edit mode
2.6 years ago
Paula ▴ 60

Hi All,

I am trying to make a stacked bar plot in ggplot2. ggplot

When I import the data to R, it adds an "X" letter in the values from the "X" axis. How can I fix it? Ideally, the x-axis should have only the values (3,40,153..). Here is the "head" from the table after I import it.

 Cluster                      Presence X3 X40 X153.5 X187.5 X213.5 X233.9 X283.4 X382
1 Genome cluster 1 Genome with dsrB gene present  0   0      0      0      0      1      1    1
2 Genome cluster 2 Genome with dsrB gene present  0   0      0      0      0      0      0    1

Here is the code:

>ready_to_plot_supermatrix <- read.csv("/Users/paularod/Documents/clusters_mapping_lever_MAGs.csv", header=TRUE)

>plotDat <- reshape::melt(ready_to_plot_supermatrix)

>ggplot(plotDat, aes(variable, Cluster, fill = Presence, alpha = value)) + geom_tile(colour = "gray50") + scale_alpha_identity(guide = "none") + coord_equal(expand = 0) + theme_bw() + theme(panel.grid.major = element_blank(), axis.text.x = element_text(angle = 45, hjust = 1)) + labs(x = "Sediment depth (cm)", y = "Genomes clustered at 97% identity", fill = "dsrB gene presence" ) + scale_fill_manual(values = coloursb)

Thank you very much!

Paula.

bar rstudio ggplot plot • 949 views
ADD COMMENT
0
Entering edit mode

What code did you use to generate the plot?

ADD REPLY
0
Entering edit mode

I've never experienced this with ggplot. Can you post the results when you head your dataframe going into ggplot? Can you also post your ggplot code so we can see your arguments in geom_bar?

ADD REPLY
3
Entering edit mode
2.6 years ago

The X is introduced by the automatic conversion of your numeric values into a factor, because you have chosen a display that warrants a categorical (discrete) instead of a continuous scale. It seems that you are using geom_tile() here?

It this heatmap what you are aiming for, or do you indeed aim for a bar plot? In any case, I would recommend preparing the data accordingly upfront (e.g. with cut() ) and then just plotting it. Even though ggplot usually does a great job using sensible defaults, you will have much more control if you just use it for visualization and do all transformations in advance.

Update:

After you edited your question and also showed your code: Yes, the problem is not the plot, but already reading in the csv file which is supposed to have numeric column names. That doesn't work.

data <- data.frame(A = 1:10, "3" = runif(10))
colnames(data)   # "A"  "X3"

One can however rename them later.

colnames(data) <- gsub("^X","",colnames(data))

I would recommend to to the renaming and also conversion from factor/character to numeric in plotDat.

ADD COMMENT
1
Entering edit mode

Heeeeey! Thank you, that worked, you're awesome!

ADD REPLY

Login before adding your answer.

Traffic: 2281 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6