我有一个日期集,其中第一列代表语言,其余列代表不同类别的使用百分比。first column repesent the language, the rest represent percentages of use
我想在一个情节中代表这张表。我认为叠条是最好的方法。在我看来,最好的方法是在x轴上表示列(除了lang),在y轴上表示百分比,作为填充表示lang。
答案 0 :(得分:1)
要使用ggplot2
,您必须以“长”格式重新排列数据,请参阅?melt
library(reshape2)
data2 <- melt(data)
以下将生成堆积条形图
library(ggplot2)
ggplot(data2, aes(variable, value, fill = Lang)) +
geom_col()
修改强>
关于您的错误:请确保您使用的是ggplot2
的最新版本 - geom_col
是我相信的最新版本。
另一种方法是使用geom_bar()
代替:
ggplot(data2, aes(variable, value, fill = Lang)) +
geom_bar(stat = 'identity')
答案 1 :(得分:1)
除了tidyr
之外,使用reshape2
只是另一种选择
library(readr)
library(ggplot2)
library(tidyverse)
df <- read_delim("Lang INT DIR ABS LND TOP VER IV IR REL
Arabic 39.05 5 0 3.33 44.76 9.76 11.9 11.43 24.29
Kiche 40.95 2.86 0 1.43 29.05 9.76 12.14 4.52 34.76
Spanish 20.45 2.25 2.86 2.04 33.74 13.7 12.07 9.41 40.08
Yucatec 39.56 6.63 13.27 7.86 49.63 11.3 16.46 15.48 25.31
Zapotec 24.79 0.43 51.28 1.07 32.26 9.83 8.76 8.33 4.06", delim = "\t")
df <- tidyr::gather(df, fields, value, -Lang)
ggplot(df, aes(x = fields, y = value, fill = Lang)) +
geom_bar(stat = 'identity')