How to change the graph structure in ggplot2?

Asked

Viewed 116 times

1

I used the following command line from ggplot2 in R to generate the attached graph:

g1 <- ggplot(count, aes(x=cluster, y=Reads)) + 
  geom_point(shape=1) + 
  facet_grid(. ~ Spp) 

ggplot(count, aes(x=cluster, y=Reads, fill=Spp, col=Spp)) + 
  geom_point(shape=1)

This graph refers to the amount of DNA sequences in each cluster of similar sequences of two species (Ca and Cp). Clusters located in the range of 0 reads of one species are clusters enriched in the other species. To try to make this more visual I would like to plot the graphic of the species Ca (the results that are in the orange color) in the upper region and keep that of the species Cp in the lower region. Literally invert the graph of Ca together, inclusive, with a scale of Y on the other opposite, also reversed. The idea is to do as in the scheme, but of course, with the data scattered and not on a straight.

structure(list(cluster = 1:20, Spp = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = 
c("Ca", "Cp"), class = "factor"), Reads = c(10808L, 16118L, 7928L, 
4171L, 6525L, 2480L, 4151L, 7870L, 2508L, 2141L, 1318L, 6072L, 6459L, 
10270L, 2068L, 55L, 1108L, 2116L, 4219L, 3939L)), row.names = c(NA, 
20L), class = "data.frame")

Does anyone know how I can do this using the package ggplot2?

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • 1

    Can you please, edit the question with the departure of dput(count) or, if the base is too large, dput(head(count, 20))?

  • 1

    Ready. Edited.

  • It looks like the answer is in the question. Try printing the chart you saved in g1. print(g1)

  • @Tomásbarcellos in G1 I have the graph of only one of the species. With Facet I plot the two species in separate graphs and with the last command I plot the two species in the same graph, which is the attached.

  • I don’t quite understand what you need. You can try to explain otherwise in the question?

  • @Tomásbarcellos I made a scheme of the idea and I think it may have become clearer, from a look at.

  • There is a problem with the data, only have Spp = "Cp" no lines with Spp = "Ca". Maybe dput(head(count, 40))? But check the data first, please.

  • Another thing. ggplot2 does is visualize data. This means your data has to be ready to be viewed or else you will have to transform it.

  • And in your case, judging by the image of the second graph, it’s as if the two groups behaved like the blue curve of the first graph.

  • Only has the Spp = Cp because I used the function facet_grid(. ~ Spp) . The distribution of the data of the two species is very similar. One would expect this pattern. And thanks @Ruibarradas your solution gave exactly the result you wanted.

Show 5 more comments

1 answer

3


The following solution uses the function sec_axis with a transformation identical to that of geom_point which reverses the values of Spp = "Ca".

library(ggplot2)

ggplot() +
  geom_point(data = subset(count, Spp = "Cp"),
             mapping = aes(cluster, Reads), color = "red") +
  geom_point(data = subset(count, Spp = "Ca"),
             mapping = aes(cluster, max(Reads) - Reads), color = "blue") +
  scale_y_continuous(sec.axis = sec_axis(~ max(.) - ., name = derive())) +
  labs(x = "Cluster", y = "Spp")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.