0

我有一个如下所示的 csv 文件,我想制作一个堆积条形图,其中 x 轴是链接列,y 轴显示频率,每个条形图根据 Freq_E 和 Freq_S 分组。当我阅读 csv 并将其提供给 barplot 时,它不起作用。我搜索了很多,但所有示例数据都是以列联表的形式。我不知道我该怎么办...

           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
4

2 回答 2

4

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

Assuming your data.frame is called "df" (as defined at the start of Codoremifa's answer), you can try the following:

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

enter image description here

于 2013-11-21T16:14:00.327 回答
3

你应该看看优秀的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")
于 2013-11-21T16:12:58.007 回答