Question

I am pretty new to R, and I have a question regarding the use of the dotplot function in the lattice package. I have multivariate data grouped by year and commodity.

Commodity   rank    Country year    tonnes
Coffee  1   Vietnam 2010    0.55
Coffee  2   Colombia    2010    0.75
Coffee  3   Brazil  2010    1.3
Coffee  4   Global Coffee   2010    2.6
Tea     5   Turkey  2010    0.2
Tea     6   Sri Lanka   2010    0.3
Tea     7   Kenya   2010    0.4
Tea     8   India   2010    1
Tea     9   China   2010    1.6
Tea     10  Global Tea  2010    3.5
Coffee  1   Vietnam 2009    0.6
Coffee  2   Brazil  2009    0.9
Coffee  3   Colombia    2009    0.9
Coffee  4   Global Coffee   2009    2.4
Tea     5   Turkey  2009    0.1
Tea     6   Sri Lanka   2009    0.1
Tea     7   Kenya   2009    0.5
Tea     8   India   2009    0.9
Tea     9   China   2009    1.7
Tea     10  Global Tea  2009    3.3

and, here's the dput() of it:

structure(list(Commodity = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Coffee", 
"Tea"), class = "factor"), rank = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Country = structure(c(10L, 
3L, 1L, 4L, 9L, 8L, 7L, 6L, 2L, 5L, 10L, 1L, 3L, 4L, 9L, 8L, 
7L, 6L, 2L, 5L), .Label = c("Brazil", "China", "Colombia", "Global Coffee", 
"Global Tea", "India", "Kenya", "Sri Lanka", "Turkey", "Vietnam"
), class = "factor"), year = c(2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L), tonnes = c(0.55, 0.75, 
1.3, 2.6, 0.2, 0.3, 0.4, 1, 1.6, 3.5, 0.6, 0.9, 0.9, 2.4, 0.1, 
0.1, 0.5, 0.9, 1.7, 3.3)), .Names = c("Commodity", "rank", "Country", 
"year", "tonnes"), class = "data.frame", row.names = c(NA, -20L
))

Using the rank variable, I was able to produce a dotplot with a specific ordering on the y-axis, with the following code

dotplot(reorder(Country,rank)~tonnes, data=commodity, pch=21, cex=1,groups=commodity$year, 
    main="Production by country" ,
    xlab="",gcolor="black")

I would like to group the variables on the y-axis by Commodity as in the example in the link below.

img
(source: statmethods.net)

This example uses dotchart and I've not been able to reproduce the results using dotplot. I'd be grateful for any help.

Was it helpful?

Solution

If you don't mind using ggplot2, this might work for you:

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_wrap(~Commodity, ncol=1)
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

enter image description here

Or, you can get it more dotplot-ish by:

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_grid(Commodity~., scales="free", space="free")
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

enter image description here

Or:

gg <- ggplot(data=dat, aes(x=tonnes, y=reorder(Country, rank)))
gg <- gg + geom_point(aes(color=Commodity))
gg <- gg + facet_wrap(~Commodity, ncol=1, space="free")
gg <- gg + labs(y="", title="Production by country")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position="none")
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top