伙计们,我不想来这里,问愚蠢的问题,并且收到弊端,但是这个阴谋杀了我。
我需要绘制一个非常简单的数据集。
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,base)中的错误:'log'对因子没有意义
我试图更改它的类,但也失败了。 我用谷歌搜索,我尝试了 - 没什么。
最令人讨厌的事情,就是在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
答案 0 :(得分:3)
由于value
中的df
列是一个因素,因此您需要使用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()
数据:强>
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")