0

I have a data set like this one: Names of mutations and two numerical variables representing values in two conditions (CIP and TIG):

Data image

I was able to plot one variable (e.g. CIP) in these mutation using the following code: Data names as "Dotchart2)

dotchart(Dotchart2$`CIP resistance`, 
   labels = rownames((Dotchart2)), pch = 16, cex = 1, pt.cex = 2)

This appeared as follows:

Figure for one variable

Since I am comparing CIP vs TIG, I would like to have the same figure but showing another dots for the TIG for the same mutation (i.e. on each horizontal mutation line, there will be two dots of different color, one for CIP value and the other for TIG value). It should appear like this figure for instance

Cleveland plot

Could any of you provide a simplified code for this ?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
M. Samir
  • 11
  • 2
  • is there any chance we could have the data (or a subset thereof) in a more useful form, e.g. as text (or the results of `dput()`) rather than a screenshot? – Ben Bolker Jun 30 '20 at 18:09
  • Sure: How can I upload a txt or excel for insatance ? – M. Samir Jun 30 '20 at 19:36
  • The best thing to do is to paste a short bit of text as an edit to your question (i.e. in place of your picture); [this question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) gives **lots** of advice – Ben Bolker Jun 30 '20 at 19:54

2 Answers2

1

I think you'll find your answer here.

In the link provided, @JoshO'Brien creates a dotchart plot using a lattice configuration:

autos_data <- read.table("~/Documents/R/test.txt", header=F)
library(lattice)
dotplot(V1~V2, data=autos_data)

This documentation does a thorough job of explaining and detailing graph styles (graph_type), data graphing (formula), and the data source (data=), resulting in the following:

library(lattice)
graph_type(formula, data=)
Alex Douglas
  • 191
  • 7
  • I don't think this quite does what the OP wants: you probably need `groups=...` to specify the colour. – Ben Bolker Jun 30 '20 at 18:07
  • Thank you. Actually in that link, he just showed each variable at seprate lines, so not allowing a comparison between CIP and TIG at once for the same mutation. – M. Samir Jun 30 '20 at 19:34
  • actually there is a code written here (https://uc-r.github.io/cleveland-dot-plots) using ggplot2 function, but I could not understand. he created an object for the 3- variable (in my case would be mutation/CIP/TIG) using the code : city_gender_rev <- supermarket %>% group_by(City, Gender) %>% summarise(Revenue = sum(Revenue, na.rm = TRUE)) %>% ungroup() %>% mutate(City = factor(City, levels = city_rev$City)) and then plotted the variables using this code : ggplot(city_gender_rev, aes(Revenue, City)) + geom_point(aes(color = Gender)) – M. Samir Jun 30 '20 at 19:38
0

To do this easily in lattice or ggplot2 you first need to convert your data to long format. I don't have a data set handy in the right format, so I took the famous iris data set and converted it to a wide-format data set called iris_wide (see code at the bottom). I'm using tidyverse here: all of this can also be done in base R.

(To understand what's going on here you should definitely examine the iris_wide and iris_long objects.)

convert from wide to long format

library(tidyverse)
iris_long <- iris_wide %>%
   pivot_longer(cols=-id,names_to="species",values_to="value")

lattice version

lattice::dotplot(id~value, data=iris_long, group=species,pch=16,
                 auto.key=TRUE)

ggplot version

ggplot(iris_long, aes(value,id,colour=species))+geom_point()

convert iris data from long to wide

To match your example, I'm selecting only two categories (species) and one variable (sepal length)

iris_wide <- (iris
    %>% filter(Species %in% c("setosa","virginica"))
    %>% select(Sepal.Length, Species)
    %>% group_by(Species)
    %>% mutate(id=seq(n()))
    %>% pivot_wider(names_from=Species, values_from=Sepal.Length)
    %>% head(10)
    %>% mutate(id=LETTERS[seq(n())])
)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453