Anakin Skywalker
Anakin Skywalker

Reputation: 2520

Multiple bar graph in ggplot 2

enter image description hereFolks, I hate to come here, asking stupid questions, and receive minuses, but this plot kills me.

I need to plot a very simple data set.

3 columns - Year, Democrats, Republicans. Year shows the year, for parties - numbers.

I swear to God, I spend several hours, trying to do it in R. Started from simple options, and go to more challenging for me.

Simple code in ggplot demonstrates an awful plot above:

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

My last attempt was melt(), but I again failed

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()

Error in Math.factor(x, base) : ‘log’ not meaningful for factors

I tried to change the class of it, but also failed. I googled, I tried SO - nothing.

The most killing thing, that in Excel I did in in 5 seconds. enter image description here

Please help.

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

Upvotes: 3

Views: 4258

Answers (1)

Mike H.
Mike H.

Reputation: 14360

Since your value column in df is a factor, you need to use as.double(as.character(df$value)). Using just as.double(df$value) will give you the numeric factor values (not what you want). However, there is some additional cleaning required since your characters will have , in them. We can gsub these out.

This should work:

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

Data:

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")

Upvotes: 4

Related Questions