[英]Dotplot groups on y-axis
我對R很dotplot
,對於在lattice
軟件包中使用dotplot
函數有疑問。 我有按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軸上對變量進行分組,如下面鏈接中的示例所示。
(來源: statmethods.net )
這個例子使用dotchart
,但我無法使用dotplot
再現結果。 我將不勝感激。
如果您不介意使用ggplot2
,那么這可能對您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:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.