0

Guys I'm trying to plot Value vs Location of my dataframe called gdp , the thing is that GGPLOT2 is not using my presorted column (with as.factor) and it's just plotting in the same way that the original indexes of the dataframe place the rows. My idea is to plot with an ascendent sorting of gdp$Values , please help.

head(gdp)

LOCATION INDICATOR  SUBJECT MEASURE FREQUENCY TIME Value Flag Codes
33      GRC EDUPUBEXP PRY_NTRY  PC_GDP         A 2014                M
5       CZE EDUPUBEXP PRY_NTRY  PC_GDP         A 2014 2.409           
29      CHL EDUPUBEXP PRY_NTRY  PC_GDP         A 2014 2.473           
34      TUR EDUPUBEXP PRY_NTRY  PC_GDP         A 2014  2.56           
23      SVK EDUPUBEXP PRY_NTRY  PC_GDP         A 2014 2.575           
24      ESP EDUPUBEXP PRY_NTRY  PC_GDP         A 2014 2.641           

gdp$Value <- factor(gdp$Value , levels = gdp$Value )

ggplot(gdp, aes(LOCATION,Value,fill=LOCATION)) + geom_bar(stat="identity")

My current plot

Update : Reproducible Example

structure(list(LOCATION = c("DNK", "NOR", "ISL", "BEL", "GBR", 
"PRT", "FIN", "ISR", "NZL", "SWE"), INDICATOR = c("EDUPUBEXP", 
"EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", 
"EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP"), SUBJECT = c("PRY_NTRY", 
"PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", 
"PRY_NTRY", "PRY_NTRY", "PRY_NTRY"), MEASURE = c("PC_GDP", "PC_GDP", 
"PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", 
"PC_GDP"), FREQUENCY = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A"), TIME = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2014L, 2014L), Value = c("4.684", "4.51", "4.491", 
"4.229", "4.218", "3.993", "3.919", "3.859", "3.811", "3.68"), 
    `Flag Codes` = c("", "", "", "", "", "", "", "", "", "")), .Names = c("LOCATION", 
"INDICATOR", "SUBJECT", "MEASURE", "FREQUENCY", "TIME", "Value", 
"Flag Codes"), row.names = c(6L, 20L, 11L, 3L, 27L, 22L, 7L, 
31L, 19L, 25L), class = "data.frame")

I'm using dplyr,ggplot2 and jsonlite as libraries.

Output of sessioninfo()

 R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Chile.1252  LC_CTYPE=Spanish_Chile.1252   
[3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Chile.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] jsonlite_1.5  ggplot2_2.2.1 dplyr_0.7.4   readxl_1.0.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     digest_0.6.15    assertthat_0.2.0
 [4] plyr_1.8.4       grid_3.4.4       cellranger_1.1.0
 [7] R6_2.2.2         gtable_0.2.0     magrittr_1.5    
[10] scales_0.5.0     pillar_1.2.1     rlang_0.2.0     
[13] lazyeval_0.2.1   bindrcpp_0.2.2   labeling_0.3    
[16] tools_3.4.4      glue_1.2.0       munsell_0.4.3   
[19] compiler_3.4.4   colorspace_1.3-2 pkgconfig_2.0.1 
[22] bindr_0.1.1      tibble_1.4.2   R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Chile.1252  LC_CTYPE=Spanish_Chile.1252   
[3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Chile.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] jsonlite_1.5  ggplot2_2.2.1 dplyr_0.7.4   readxl_1.0.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     digest_0.6.15    assertthat_0.2.0
 [4] plyr_1.8.4       grid_3.4.4       cellranger_1.1.0
 [7] R6_2.2.2         gtable_0.2.0     magrittr_1.5    
[10] scales_0.5.0     pillar_1.2.1     rlang_0.2.0     
[13] lazyeval_0.2.1   bindrcpp_0.2.2   labeling_0.3    
[16] tools_3.4.4      glue_1.2.0       munsell_0.4.3   
[19] compiler_3.4.4   colorspace_1.3-2 pkgconfig_2.0.1 
[22] bindr_0.1.1      tibble_1.4.2   
Community
  • 1
  • 1
schwerter2
  • 11
  • 4

1 Answers1

0

2nd Update

Let me know if this works:

library(ggplot2)
library(dplyr)
library(readxl)
library(jsonlite)

gdp <- structure(list(LOCATION = c("DNK", "NOR", "ISL", "BEL", "GBR", 
"PRT", "FIN", "ISR", "NZL", "SWE"), INDICATOR = c("EDUPUBEXP", 
"EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", 
"EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP", "EDUPUBEXP"), SUBJECT = c("PRY_NTRY", 
"PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", "PRY_NTRY", 
"PRY_NTRY", "PRY_NTRY", "PRY_NTRY"), MEASURE = c("PC_GDP", "PC_GDP", 
"PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", "PC_GDP", 
"PC_GDP"), FREQUENCY = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A"), TIME = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2014L, 2014L), Value = c("4.684", "4.51", "4.491", 
"4.229", "4.218", "3.993", "3.919", "3.859", "3.811", "3.68"), 
    `Flag Codes` = c("", "", "", "", "", "", "", "", "", "")), .Names = c("LOCATION", 
"INDICATOR", "SUBJECT", "MEASURE", "FREQUENCY", "TIME", "Value", 
"Flag Codes"), row.names = c(6L, 20L, 11L, 3L, 27L, 22L, 7L, 
31L, 19L, 25L), class = "data.frame")

gdp <- gdp %>%
       mutate(Value = as.numeric(Value))

# Unsorted
ggplot(data=gdp, mapping=aes(x=LOCATION, y=Value, fill=LOCATION))+
    geom_bar(stat = "identity")

#Sorted
ggplot(data=gdp, mapping=aes(x=reorder(LOCATION, -Value), y=Value, fill=LOCATION))+
    geom_bar(stat = "identity")

UPDATED ANSWER

I wasn't able to reproduce your error because I am not entirely sure how your environment is set up. If you're interested, here is a post that describes how to create a great reproducible example. It will allow people to help answer your question faster, but more importantly, I've found that through the process of creating a reproducible example, I usually solve the problem on my own.

Anyway, start a new R session and run the following code:

library(dplyr)
library(ggplot2)

gdp <- tribble(~LOCATION, ~Value,
           "A", 2.3,
           "B", 4,
           "C", 4,
           "D", 3.5,
           "E", 2,
           "F", 3)

ggplot(data=gdp, mapping=aes(x=LOCATION, y=Value, fill=LOCATION))+
    geom_bar(stat = "identity")

Unsorted Output Image

Then, if that works, try running this code:

ggplot(data=gdp, mapping=aes(x=reorder(LOCATION, -Value), y=Value, fill=LOCATION))+
    geom_bar(stat = "identity")

Sorted Output Image

If that works, then try something similar with your data. If that doesn't work, try to create a reproducible example and include the output from the following code:

sessionInfo()

Let me know what happens.

ORIGINAL ANSWER

It looks like you are sorting the value, but not sorting the location BY the value. It would be nice to have a reproducible example, but does this help?

ggplot(data = gdp, mapping = aes(x = reorder(LOCATION, -Value), y = Value, fill = LOCATION) + 
geom_bar(stat = "identity")
  • It throws this warnings: **Warning messages:** 1: In Ops.factor(Value) : ‘-’ not meaningful for factors 2: In Ops.factor(Value) : ‘-’ not meaningful for factors By the way, what do you mean about the reproducible example? i can give you the json containing the dataframe. – schwerter2 Apr 02 '18 at 21:57
  • I've updated my original answer to hopefully address some of your follow up. I don't think it is entirely necessary to factor your Value variable, since it is already numeric. But I factored the Value variable in my example above and it worked the same. If you are set on factoring, try factoring the LOCATION variable and use the Value variable to set the levels, which `reorder()` _kind of_ does. Let me know if any of this helps. – Christian Million Apr 03 '18 at 15:48
  • I'm making a new comment trying to make a reproducible example. – schwerter2 Apr 05 '18 at 18:23
  • So, in the reproducible example that you provided me, your "Value" elements are being considered characters. A simply way to convert it to numeric (using dplyr) would be `gdp <- mutate(gdp, Value = as.numeric(Value))`. I'll update my answer again so you can see all of the code. – Christian Million Apr 06 '18 at 19:17
  • OR: `ggplot(data=gdp, mapping=aes(x=reorder(LOCATION, -as.numeric(Value)), y=Value, fill=LOCATION))+ geom_bar(stat = "identity")` – Christian Million Apr 06 '18 at 19:21