位置为“ dodge”的条形图,已重新排序并分组

时间:2018-07-31 14:38:58

标签: r ggplot2

我正在尝试将以下数据绘制为躲避的条形图:

Ld /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences\ Resident.build/Debug-iphoneos/Safences\ Resident.build/Objects-normal/arm64/Safences\ Resident normal arm64
cd /Users/inayath/Desktop/safencesResidentAppAndroid/platforms/ios
export IPHONEOS_DEPLOYMENT_TARGET=9.0
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.4.sdk -L/Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos -L/Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/GoogleToolboxForMac -L/Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/Protobuf -L/Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/nanopb -F/Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos -F/Users/inayath/Desktop/safencesResidentAppAndroid/platforms/ios/Pods/FirebaseAnalytics/Frameworks -F/Users/inayath/Desktop/safencesResidentAppAndroid/platforms/ios/Pods/FirebaseCore/Frameworks -F/Users/inayath/Desktop/safencesResidentAppAndroid/platforms/ios/Pods/FirebaseInstanceID/Frameworks -F/Users/inayath/Desktop/safencesResidentAppAndroid/platforms/ios/Pods/FirebaseMessaging/Frameworks -filelist /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences\ Resident.build/Debug-iphoneos/Safences\ Resident.build/Objects-normal/arm64/Safences\ Resident.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -miphoneos-version-min=9.0 -dead_strip -Xlinker -object_path_lto -Xlinker /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences\ Resident.build/Debug-iphoneos/Safences\ Resident.build/Objects-normal/arm64/Safences\ Resident_lto.o -Xlinker -no_deduplicate -fobjc-arc -fobjc-link-runtime -ObjC -ObjC -lGoogleToolboxForMac -lProtobuf -lc++ -lnanopb -lsqlite3 -lz -framework AddressBook -framework FirebaseAnalytics -framework FirebaseCore -framework FirebaseCoreDiagnostics -framework FirebaseInstanceID -framework FirebaseMessaging -framework FirebaseNanoPB -framework StoreKit -framework SystemConfiguration /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/libCordova.a -weak_framework AddressBook -weak_framework AddressBookUI -weak_framework SystemConfiguration -framework CoreTelephony -weak_framework MessageUI -lPods-Safences\ Resident -Xlinker -dependency_info -Xlinker /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences\ Resident.build/Debug-iphoneos/Safences\ Resident.build/Objects-normal/arm64/Safences\ Resident_dependency_info.dat -o /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences\ Resident.build/Debug-iphoneos/Safences\ Resident.build/Objects-normal/arm64/Safences\ Resident

duplicate symbol _OBJC_CLASS_$_CDVLogger in:
    /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences Resident.build/Debug-iphoneos/Safences Resident.build/Objects-normal/arm64/CDVLogger.o
    /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/libCordova.a(CDVLogger.o)
duplicate symbol _OBJC_METACLASS_$_CDVLogger in:
    /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Intermediates.noindex/Safences Resident.build/Debug-iphoneos/Safences Resident.build/Objects-normal/arm64/CDVLogger.o
    /Users/inayath/Library/Developer/Xcode/DerivedData/Safences_Resident-bohfyzovmoapaxbnzlsayigtqxpl/Build/Products/Debug-iphoneos/libCordova.a(CDVLogger.o)
ld: 2 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

在x轴上,我正在查看要显示的单词,在y轴上,是计数n。但是,我想将这10个最热门的单词并排比较。

我尝试了以下代码,显然没有得到预期的结果:

structure(list(word = c("evil", "intelligence", "medical", "revolution", 
"change", "dominate", "deplorable", "broken", "fight", "government", 
"agreement", "real", "intelligence", "medical", "system", "mainstay", 
"expert", "intelligent", "excellence", "guide"), sentiment = c("fear", 
"fear", "fear", "fear", "fear", "fear", "fear", "fear", "fear", 
"fear", "trust", "trust", "trust", "trust", "trust", "trust", 
"trust", "trust", "trust", "trust"), n = c(304L, 257L, 159L, 
152L, 127L, 118L, 96L, 92L, 81L, 79L, 300L, 297L, 257L, 159L, 
131L, 92L, 83L, 78L, 75L, 74L)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L), vars = "sentiment", drop = TRUE, indices = list(
    0:9, 10:19), group_sizes = c(10L, 10L), biggest_group_size = 10L, labels = structure(list(
    sentiment = c("fear", "trust")), class = "data.frame", row.names = c(NA, 
-2L), vars = "sentiment", drop = TRUE, indices = list(c(1L, 2L, 
3L, 4L, 5L, 8L, 9L, 11L, 15L, 18L), c(0L, 6L, 7L, 10L, 12L, 13L, 
14L, 16L, 17L, 19L)), group_sizes = c(10L, 10L), biggest_group_size = 10L))

我想看到的是邪恶与共识这两个词,然后是智力和真实等等。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以尝试

library(tidyverse)
d %>% 
  group_by(sentiment) %>% 
  mutate(index=1:n()) %>% 
  ggplot(aes(x=factor(index), y=n, fill=sentiment)) +
    geom_col(position = position_dodge(width = 0.9)) +
    geom_text(aes(y=0,label=word), hjust=0,
              position = position_dodge(width = 0.9))+
      coord_flip()

enter image description here

或者您可以尝试facet_wrap解决方案

d %>% 
    group_by(sentiment) %>% 
    mutate(index=1:n()) %>% 
    ggplot(aes(x=word, y=n,fill=sentiment)) +
    geom_col(position = position_dodge(width = 0.9)) +
    coord_flip() + 
    facet_wrap(~index, scale="free_y", strip.position="right", ncol=1) + 
    theme(strip.text = element_blank())

enter image description here

在两种解决方案中,您都需要使用例如tidyverse定义哪些词属于同一词。

答案 1 :(得分:0)

您可以尝试将word变成factor

x$word <- factor(x$word, levels = c("evil", "agreement", "intelligence", "real","medical", "revolution", 
                                "change", "dominate", "deplorable", "broken", "fight", "government", 
                                "intelligence", "medical", "system", "mainstay", 
                                "expert", "intelligent", "excellence", "guide"),
             labels  = c("evil", "agreement", "intelligence", "real","medical", "revolution", 
                         "change", "dominate", "deplorable", "broken", "fight", "government", 
                         "intelligence", "medical", "system", "mainstay", 
                         "expert", "intelligent", "excellence", "guide"))


ggplot(x, aes(fill=sentiment, y=n, x=word)) +
       geom_bar(position="dodge", stat="identity")+coord_flip()