我对R很新,我对dotplot
包中使用lattice
函数有疑问。我有按year
和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
并且,这是dput()
:
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
))
使用rank
变量,我能够在y轴上生成一个具有特定排序的点图,具有以下代码
dotplot(reorder(Country,rank)~tonnes, data=commodity, pch=21, cex=1,groups=commodity$year,
main="Production by country" ,
xlab="",gcolor="black")
我想通过Commodity
对y轴上的变量进行分组,如下面链接中的示例所示。
img http://www.statmethods.net/graphs/images/dotplot2.png
此示例使用dotchart
,但我无法使用dotplot
重现结果。我会感激任何帮助。
答案 0 :(得分:0)
如果您不介意使用ggplot2
,这可能对您有用:
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
或者,您可以获得更多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
或者:
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