2015-09-29 51 views
1

我遇到了用這些數據給分組的dotplots着色的麻煩(奇怪的是,我無法用ggplot2附帶的數據集中的一個重現該問題,因此我對額外的步驟表示歉意)。如何在ggplot2中按組來繪製彩色dotplots?

data <- data.frame(
    Period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L), 
    Rate = c(0, 10, 10, 100, 20, 10, 20, 10, 5, 100, 0, 50, 50, 100, 20, 100, 0, 5, 5, 0, 0, 50, 10, 100, 15, 50, 0, 0, 5, 0, 100, 50, 0, 100, 0, 50, 0, 0, 5, 0, 100, 100, 50, 100, 0, 0, 0, 0, 5, 100, 100, 0, 0, 100, 0, 0, 0, 0, 0, 0, 100, 0, 0, 100, 0, 100, 0, 0, 100, 10, 100, 0, 0, 100, 0, 10, 5, 0, 100, 0, 100, 0, 100, 100, 0, 10, 5, 0, 0, 100, 100, 0, 100, 100, 30, 10, 10, 100, 0, 100, 100, 0, 0, 0, 10, 10, 0, 10, 100, 50, 0, 0, 100, 0, 0, 10, 20, 100, 100, 100, 0, 60, 0, 0, 0, 100, 50, 100, 100, 100, 100, 100, 20, 0, 0, 20, 100, 10, 0, 0, 100, 100, 30, 0, 0, 10, 100, 0, 0, 0), 
    Subject = c("1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6", "1", "10", "11", "12", "13", "14", "15", "3", "5", "6") 
) 
g <- ggplot(data = data, mapping = aes(x = factor(Period), y = Rate, fill = Subject, group = Period)) 
g + geom_dotplot(binwidth = 1, binaxis = "y", stackdir = "center") 

dotplot

這是所有罰款,只是奇怪的是,該點不保留fill=Subject顏色。如何爲Subject打上顏色?

更新

沒有group = Period,被建議在評論中,填充顏色被保留,但沒有一個真正的點陣圖 - 所有的點重疊。

nongrouped

+1

你只需要刪除'組= Period' – beetroot

+0

是的,但沒有'組= Period',它不是一個真正的點陣圖 - 他們都重疊。查看更新的問題。 – maxheld

+0

does'position =「dodge」'help? – beetroot

回答

2

EDIT2看來,這個問題也得到了解決here。我將這個問題標記爲重複的問題。

使用fill直接geom_dotplot似乎工作:

data$Period <- as.factor(data$Period) 
g <- ggplot(data, aes(x = Period, y = Rate, fill = Subject, group = Period)) 
g + geom_dotplot(binwidth = 1, binaxis = "y", stackdir = "center", 
       fill = data$Subject) 

image

顯然,在ggplotfill可能再被淘汰。

編輯

添加position = "dodge"和省略group = Period也產生了類似的情節:

ggplot(data, aes(x = factor(Period), y = Rate, fill = factor(Subject))) + 
    geom_dotplot(binaxis = "y", stackdir = "center", position = "dodge", binwidth = 2) 

image2

是這個嗎?

+0

mmh是的,但然後圖例消失(和顏色將不得不被手動着色,正確?) 我只是不明白爲什麼'geom_dotplot'不尊重'fill = Subject'美學映射在這種情況下,它是*假設*根據文檔理解:http://docs.ggplot2.org/0.9.3.1/geom_dotplot.html – maxheld

+0

@maxheld'position =「dodge」'然後似乎做伎倆?我編輯了我的答案。那是你要的嗎? –

+1

@maxheld我把這個問題標記爲[this。]的重複(http://stackoverflow.com/questions/31557973/how-to-use-ggplot2s-geom-dotplot-with-both-fill-and-group )希望有所幫助 - 這似乎是一個錯誤。 –

1

嘗試geom_jitter

g + geom_jitter(binwidth = 1, binaxis = "y", stackdir = "center", aes(group = Subject, color = Subject), size = 4) 

enter image description here

+0

是的,我也試過,謝謝 - 它非常接近,但不是真正的點陣圖。 我只是不明白爲什麼'geom_dotplot'在這種情況下不會接受'fill = Subject'美學映射,儘管它被記錄爲這樣做。 – maxheld