如何将此数据绘制为分组条形图?

时间:2021-02-17 04:17:31

I would like to plot this data in R

我想在R中绘制这些数据

item    response    Freq
count   -99         0.000
count   -88         0.000
count   -77         0.050
count   1           0.039
count   2           0.117
count   3           0.408
count   4           0.385
read    -99         0.011
read    -88         0.000
read    -77         0.117
...      ...        ...

the x axis should be item (so count, read etc., altogether 19 items), on the y axis should be the Frequencies for each response (so 1,2,3,4,-77,-88,-99). So this would be a "grouped" barchart essentially.

x轴应该是项目(所以计数,读取等,共19项),y轴应该是每个响应的频率(所以1,2,3,4,-77,-88,-99)。所以这将是一个“分组”的条形图。

I am not able to plot this grouped bar chart using R. Until now, i tried it with:

我无法使用R绘制这个分组的条形图。到现在为止,我尝试使用:

library(ggplot2)
ggplot(Data, aes(response, Freq*100, color = item)) +
  geom_point(size = 4) + geom_line(size = 1, aes(group = item)) + ggtitle("Items") + 
  ylab("Frequencies %") + xlab("responses") 


require(lattice)
barchart(Freq*100 ~ item, groups=response, Data, xlab="Items", ylab="Frequencies %")

Thanks a lot!

非常感谢!

3 个解决方案

#1


1  

You can use following code:

您可以使用以下代码:

library(ggplot2)
ggplot(dat, aes(x=1, y=Freq, fill=factor(response))) + 
  geom_bar(stat="identity", position="dodge") +
  facet_grid(~item) +
  scale_fill_grey() +
  theme_bw()

如何将此数据绘制为分组条形图?

The position="dodge" aligns the bars side-by-side. The facet_grid uses a new plot for each "item" (i.e. each group that is indicated by the variable named "item"). The scale_fill_grey() applies grey colours to the bars and the theme_bw() removes the grey background.

position =“dodge”并排排列。 facet_grid为每个“item”使用一个新的图(即每个由名为“item”的变量指示的组)。 scale_fill_grey()将灰色应用于条形,theme_bw()移除灰色背景。

#2


0  

Try this:

尝试这个:

dat <- read.table(header=TRUE, text="item    response    Freq
count   -99         0.000
count   -88         0.000
count   -77         0.050
count   1           0.039
count   2           0.117
count   3           0.408
count   4           0.385
read    -99         0.011
read    -88         0.000
read    -77         0.117")

Using ggplot2:

使用ggplot2:

library(ggplot2)
ggplot(dat, aes(x=item, y=Freq, fill=factor(response))) + 
    geom_bar(stat="identity")

如何将此数据绘制为分组条形图?

#3


0  

Try for black-white-grey graph:

尝试黑白灰图:

barplot(with(ddf, tapply(Freq , list(response, item), mean)), beside=T, xlab="item", ylab="Freq")

如何将此数据绘制为分组条形图?

#1


1  

You can use following code:

您可以使用以下代码:

library(ggplot2)
ggplot(dat, aes(x=1, y=Freq, fill=factor(response))) + 
  geom_bar(stat="identity", position="dodge") +
  facet_grid(~item) +
  scale_fill_grey() +
  theme_bw()

如何将此数据绘制为分组条形图?

The position="dodge" aligns the bars side-by-side. The facet_grid uses a new plot for each "item" (i.e. each group that is indicated by the variable named "item"). The scale_fill_grey() applies grey colours to the bars and the theme_bw() removes the grey background.

position =“dodge”并排排列。 facet_grid为每个“item”使用一个新的图(即每个由名为“item”的变量指示的组)。 scale_fill_grey()将灰色应用于条形,theme_bw()移除灰色背景。

#2


0  

Try this:

尝试这个:

dat <- read.table(header=TRUE, text="item    response    Freq
count   -99         0.000
count   -88         0.000
count   -77         0.050
count   1           0.039
count   2           0.117
count   3           0.408
count   4           0.385
read    -99         0.011
read    -88         0.000
read    -77         0.117")

Using ggplot2:

使用ggplot2:

library(ggplot2)
ggplot(dat, aes(x=item, y=Freq, fill=factor(response))) + 
    geom_bar(stat="identity")

如何将此数据绘制为分组条形图?

#3


0  

Try for black-white-grey graph:

尝试黑白灰图:

barplot(with(ddf, tapply(Freq , list(response, item), mean)), beside=T, xlab="item", ylab="Freq")

如何将此数据绘制为分组条形图?