在ggplot2中创建条形图,描绘数据集

时间:2017-09-22 00:49:37

标签: r ggplot2

我有一个与此类似的数据集:

Area             Chemical   Machinery   Other
Abilene TX       Yes        No          Yes
Akron OH         Yes        No          No
Albany GA        Yes        Yes         No
Albuquerque NM   No         Yes         Yes
Alexandria LA    Yes        No          Yes

我需要使用 ggplot2 来制作条形图,以显示每列中“yeses”的数量。因此,最终条形图在x轴上将有三列,“化学”的y轴值为4,“机械”为2,“其他”为3。

仍然是ggplot2的新手,也不确定如何在每列中清晰地找到特定值的计数(在本例中为“yeses”的数量)并绘制图形。谢谢!

2 个答案:

答案 0 :(得分:2)

如果您将数据以宽格式(多列)转换为长格式(更少列,更多行),这会更容易

OK

正如@steveb指出的那样,您可以使用Error:Android Source Generator: Error: Can't find bundle for base name messages.AndroidJpsBundle, locale en_US java.util.MissingResourceException: Can't find bundle for base name messages.AndroidJpsBundle, locale en_US at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387) at java.util.ResourceBundle.getBundle(ResourceBundle.java:773) at org.jetbrains.jps.android.AndroidJpsBundle.getBundle(AndroidJpsBundle.java:22) at org.jetbrains.jps.android.AndroidJpsBundle.message(AndroidJpsBundle.java:32) at org.jetbrains.jps.android.AndroidSourceGeneratingBuilder.runAaptCompiler(AndroidSourceGeneratingBuilder.java:971) at org.jetbrains.jps.android.AndroidSourceGeneratingBuilder.doBuild(AndroidSourceGeneratingBuilder.java:210) at org.jetbrains.jps.android.AndroidSourceGeneratingBuilder.build(AndroidSourceGeneratingBuilder.java:114) at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1263) at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:940) at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:1012) at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:903) at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:736) at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:384) at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:192) at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:138) at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:295) at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:125) at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler.lambda$channelRead0$0(BuildMain.java:236) at org.jetbrains.jps.service.impl.SharedThreadPoolImpl.lambda$executeOnPooledThread$0(SharedThreadPoolImpl.java:42) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

来简化一点
library(tidyr)
library(dplyr)
yes <- df %>%
  select(-Area) %>%
  gather() %>%
  group_by(key) %>%
  summarise(value = sum(value=="Yes"))

# A tibble: 3 x 2
        # key value
      # <chr> <int>
# 1  Chemical     4
# 2 Machinery     2
# 3     Other     3

library(ggplot2)
ggplot(yes, aes(x=key, y=value)) + 
  geom_bar(stat="identity")

答案 1 :(得分:0)

Base-R使用colSums

进入图表的简短方法
n_yes <- data.frame(type = names(df[, -1]), 
                    total_yes = colSums(df[, -1] == "Yes"))

ggplot(n_yes, aes(x = type, y = total_yes)) + 
  geom_bar(stat = "identity")

enter image description here