I imported information from an excel file into RStudio. When I tried to create a bar chart out of two of the set's columns it didn't work.
ggplot(data = Encuesta_BIDVOX) geom_bar(mapping = aes(x=Cargo, fill=1_PC_Estandarizado))
The Console's output showed:
Error: unexpected input in "ggplot(data = Encuesta_BIDVOX) geom_bar(mapping = aes(x=Cargo, fill=1_"
I realized that the issue was that the Variable's name (the column's name) which I'm using to fill the bar starts with a number (I tried with other columns with no numbers starting its name and it worked fine).
So the question is:
Is there a way to command R to ignore that the Columns name is starting with a number, and I can use it in the ggplot function without ending in an error?
This would be of immense help since most of the columns (over 48) start with a number...
Please keep answers as simple as possible since as you already noticed I'm new at R. Thanks!
CodePudding user response:
I suggest fixing this as far upstream as possible, potentially in the source file, or upon import, or if not then before you go to ggplot. janitor::clean_names()
offers a nice interface for doing that and controlling what the new, syntactical column names look like.
uhoh <- data.frame(`1_` = 1,
`2_` = 2,
`10_` = 3,
check.names = FALSE)
uhoh
# 1_ 2_ 10_
#1 1 2 3
uhoh %>%
janitor::clean_names() %>%
ggplot()
geom_bar(aes(x1, fill = x10))
CodePudding user response:
Just using ``
may helps(note that it's not '
)
iris3 <- iris
names(iris3) <- c("Sepal.Length", "Sepal.Width" , "Petal.Length" ,"Petal.Width" , '1_')
iris3 %>%
ggplot(aes(Sepal.Length, Sepal.Width, color = `1_`))
geom_point()
CodePudding user response:
You can use make.names
, which is used by e.g. data.frame()
to make sure names are valid.
# make a dataframe with invalid names
x <- data.frame('1a' = 1, '2b' = 1, '_c' = 1, check.names = FALSE)
# make any invalid names valid
names(x) <- make.names(names(x), unique = TRUE)
But, it is almost always better to fix the names where you are creating them (e.g. most functions to read in data have arguments to make sure the names are checked and valid).