简体   繁体   中英

R column dataframe names number

I have a dataframe like this

geo 2001    2002
Spain   21  23
Germany 34  50
Italy   57  89
France  19  13

As the names of 2nd an 3rd column are considered as number I'm not able to get a bar chart wth ggplot2. Is there any solution to set the column names to be considered as text?

data

pivot_dat <- read.table(text="geo 2001    2002
Spain   21  23
Germany 34  50
Italy   57  89
France  19  13",strin=F,h=T)
pivot_dat <- setNames(pivot_dat,c("geo","2001","2002"))

Here's how to do it :

library(ggplot2)
ggplot(pivot_dat, aes(x = geo, y = `2002`)) + geom_col()+ coord_flip()

by using ticks instead of quotes/double quotes you make sure you pass a name to the function and not a string.

If you use quotes, ggplot will convert this character value to a factor and recycle it, so all bars will have the same length of 1 , and a label of value "2002" .

Note 1 :

You might want to learn the difference between geom_col and geom_bar :

?ggplot2::geom_bar

In short geom_col is geom_bar with stat = "identity" , which is what you want here since you want to show on your plot the raw values from your table.

Note 2 :

aes_string can be used to give string instead of names but here it doesn't work as "2002" is evaluated as a number :

ggplot(pivot_dat, aes_string(x = "geo", y = "2002")) + 
  geom_col()+ coord_flip() # incorrect output

ggplot(pivot_dat, aes_string(x = "geo", y = "`2002`")) + 
  geom_col()+ coord_flip() # correct output

Without an example to see exactly what your problem is, and what you want, it is hard to give you a perfect answer. But here's the thing.

You can do a geom_bar with numeric data. There are 3 possible ways I see that you could have problems (but I may not be able to guess every way.

First, let's set up the r for plotting.

library(readr)
library(ggplot2)

test <- read_csv("geo,2001,2002
Spain,21,23
Germany,34,50
Italy,57,89
France,19,13")

Next, let's make the first mistake...incorrectly calling the column name. In the next example I will tell ggplot to make a bar of the number 2001. Not the column 2001 ! r has to guess whether we mean 2001 or whether we mean the object 2001 . By default it always picks the number instead of the column.

ggplot(test) +
  geom_bar(aes(x=2001))

在此处输入图片说明

Ok, that just gives you a bar at 2001...because you gave it a single number input instead of a column. Let's fix that. Use the right facing quotes `` to identify the column name 2001 instead of the number 2001.

ggplot(test) +
  geom_bar(aes(x=`2001`))

在此处输入图片说明

This creates a perfectly workable bar chart. But maybe you don't want the spaces? That's the only possible reason you would use text instead of a number. But you want text so I'm going to show you how to use as.factor to do something similar (and more powerful).

ggplot(test) +
  geom_bar(aes(x=as.factor(`2001`)))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM