[英]Stacked bar plot in R
I have a csv file like below, I want to make a stacked bar plot that x-axis is link column and and y-axis shows the frequency and each bar is grouped based on Freq_E and Freq_S. 我有一个csv文件,如下所示,我想制作一个堆叠的条形图,其中x轴是链接列,y轴显示频率,并且每个条基于Freq_E和Freq_S进行分组。 when I read the csv and give it to barplot it doesn't work.
当我阅读csv并将其提供给barplot时,它不起作用。 I searched a lot but all examples data is in form of contingency table.
我搜索了很多,但所有示例数据都是列联表的形式。 I donno what should I do...
我不知道该怎么办...
link Freq_E Freq_S
1 tube.com 214 214
2 list.net 120 120
3 vector.com 119 118
4 4cdn.co 95 96
"It doesn't work" is not an error message in R that I'm familiar with, but I'm guessing your problem is that you are trying to use barplot
on a data.frame
while you should be using a matrix
or a vector
. “它不工作”是不是R中的错误消息我很熟悉,但我猜你的问题是,你要使用
barplot
在data.frame
,而你应该用一个matrix
或vector
。
Assuming your data.frame
is called "df" (as defined at the start of Codoremifa's answer), you can try the following: 假设您的
data.frame
称为“ df”(在Codoremifa答案的开头定义),则可以尝试以下操作:
x <- as.matrix(df[-1]) ## Drop the first column since it's a character vector
rownames(x) <- df[, 1] ## Add the first column back in as the rownames
barplot(t(x)) ## Transpose the new matrix and plot it
You should look at the excellent ggplot2
library, try this code snippet for your example - 您应该查看出色的
ggplot2
库,请为您的示例尝试以下代码片段-
df <- read.table(textConnection(
'link Freq_E Freq_S
tube.com 214 214
list.net 120 120
vector.com 119 118
4cdn.co 95 96'), header = TRUE)
library(ggplot2)
library(reshape2)
df <- melt(df, id = 'link')
ggplot(
data = df,
aes(
y = value,
x = link,
group = variable,
shape = variable,
fill = variable
)
) +
geom_bar(stat = "identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.