2017-04-11 61 views
2

enter image description here夥計們,我討厭來這裏,問愚蠢的問題,並接受錯誤,但這種情節殺死了我。ggplot 2中的多條形圖

我需要繪製一個非常簡單的數據集。

3列 - 年,民主黨人,共和黨人。 年份顯示了當年的聚會 - 數字。

我向上帝發誓,我花了好幾個小時,試圖在R. 從簡單的選項開始,並對我更具挑戰性。

在ggplot

簡單的代碼演示了上面一個可怕的陰謀:

ggplot(df,aes(x = Year,y = value)) + 
geom_bar(aes(fill = variable), stat = "identity", position = "dodge") 

我最後的嘗試是融化(),但我又失敗了

df <- melt(platforms[,c("Year", "Democrats", "Republicans")], id.vars = 1) 
ggplot(df,aes(x = Year,y = value)) + 
    geom_bar(aes(fill = variable), stat = "identity", position = "dodge") + scale_y_log10() 

錯誤Math.factor(X ,基地):'日誌'沒有意義的因素

我試圖改變類它,但也失敗了。 我用Google搜索了一下,我試過了 - 什麼也沒有。

在Excel中我殺死的東西最多,在5秒內完成。 enter image description here

請幫忙。

UPD。

platforms <- read.csv("Platforms(f3).csv", header = TRUE) 

Year <- platforms$Year 
Dems <- platforms$Democrats 
GOP <- platforms$Republicans 


Year Democrats Republicans 
1 2016 26,058  35,467 
2 2012 26,558  30,563 
3 2008 25,997  23,585 
4 2004 17,751  41,156 
5 2000 24,220  34,555 
6 1996 18,107  27,817 
7 1992  8,555  28,531 
8 1988  4,838  35,838 
9 1984 37,231  27,383 
10 1980 38,180  34,558 
11 1976 21,202  20,463 
12 1972 25,615  24,407 
13 1968 16,791  10,013 
14 1964 20,126  8,740 
15 1960 16,098  10,680 
16 1956 12,839  11,390 
17 1952  8,878  5,988 
18 1948  4,256  2,739 
19 1944  1,366  4,216 
20 1940  4,667  3,284 
21 1936  2,310  3,030 
22 1932  1,489  7,832 
23 1928  5,613  7,401 
24 1924  5,819  5,227 
25 1920  7,118  6,391 
26 1916  4,570  2,377 
27 1912  4,696  3,393 
28 1908  4,555  4,092 
29 1904  3,058  2,254 
30 1900  2,580  2,299 
31 1896  1,929  1,933 
32 1892  2,510  1,342 
33 1888  1,379  2,391 
34 1884  2,695  1,538 
35 1880  692  1,471 
36 1876  1,867  1,363 
37 1872  633  1,270 
38 1868  1,422   924 
39 1864  506   895 
40 1860  372  1194 
41 1856  2,433   952 
+1

您收到downvotes的原因可能是因爲你沒有張貼重複的例子。你可以在這裏發佈「dput(平臺)」嗎?沒有樣本數據很難提供幫助。 –

回答

2

由於您在dfvalue列是一個因素,你需要使用as.double(as.character(df$value))。只使用as.double(df$value)會給你數字因子值(不是你想要的)。但是,由於你的角色會有,,因此需要進行一些額外的清理。我們可以gsub這些了。

這應該工作:

df <- melt(platforms[,c("Year", "Democrats", "Republicans")], id.vars = 1) 
df$value <- as.double(gsub(",","",as.character(df$value))) 


ggplot(df,aes(x = Year,y = value)) + 
    geom_bar(aes(fill = variable), stat = "identity", position = "dodge") + scale_y_log10() 

enter image description here

數據:

platforms <- structure(list(Year = c(2016L, 2012L, 2008L, 2004L, 2000L, 1996L, 
1992L, 1988L, 1984L, 1980L, 1976L, 1972L, 1968L, 1964L, 1960L, 
1956L, 1952L, 1948L, 1944L, 1940L, 1936L, 1932L, 1928L, 1924L, 
1920L, 1916L, 1912L, 1908L, 1904L, 1900L, 1896L, 1892L, 1888L, 
1884L, 1880L, 1876L, 1872L, 1868L, 1864L, 1860L, 1856L), Democrats = c("26,058", 
"26,558", "25,997", "17,751", "24,220", "18,107", "8,555", "4,838", 
"37,231", "38,180", "21,202", "25,615", "16,791", "20,126", "16,098", 
"12,839", "8,878", "4,256", "1,366", "4,667", "2,310", "1,489", 
"5,613", "5,819", "7,118", "4,570", "4,696", "4,555", "3,058", 
"2,580", "1,929", "2,510", "1,379", "2,695", "692", "1,867", 
"633", "1,422", "506", "372", "2,433"), Republicans = c("35,467", 
"30,563", "23,585", "41,156", "34,555", "27,817", "28,531", "35,838", 
"27,383", "34,558", "20,463", "24,407", "10,013", "8,740", "10,680", 
"11,390", "5,988", "2,739", "4,216", "3,284", "3,030", "7,832", 
"7,401", "5,227", "6,391", "2,377", "3,393", "4,092", "2,254", 
"2,2991", "1,933", "1,342", "2,391", "1,538", "1,471", "1,363", 
"1,270", "924", "895", "1194", "952")), .Names = c("Year", "Democrats", 
"Republicans"), row.names = c(NA, -41L), class = "data.frame") 
+0

非常感謝。我現在唯一的擔憂 - 你的情節沒有展示數據的想法。例如,數值在1000到35000之間,我們在左側肯定會看到1000,但是35000條應該更高。我會假設我只需要擴展Y軸的極限。 – Oleksiy

+0

我使用您提供的示例代碼。你的代碼中有一個'scale_y_log10()',我繼承了它。如果你願意,這可以很容易地被丟棄 –