1

I'm trying to make a plot using ggplot2 with data from a csv file with names that contain accents like, for example, "México"

The problem is that the outcome is a plot that replaces accents with other characters.

data <- read.csv("Vic102(2).csv", sep = ",", header = TRUE, encoding = "latin1")
data2 <- melt(data, id = "Pais")
ggplot(data2, aes(Pais, y = value , fill = variable)) + geom_bar() + coord_flip()

I have also tried

data <- read.csv("Vic102(2).csv", sep = ",", header = TRUE, encoding = "utf-8")
data2 <- melt(data, id = "Pais")
ggplot(data2, aes(Pais, y = value , fill = variable)) + geom_bar() + coord_flip()

On the y labels one reads: M<8e>xico or M.xico

I have also tried replacing the labels with no success:

data2$Pais[11]<-"México"
Warning message:
In `[<-.factor`(`*tmp*`, 11, value = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,  :
invalid factor level, NA generated

I am using Rstudio with R version 3.0.0 and my computer is a MAC OS X 10.8.5

Thank you!

user2809432
  • 142
  • 1
  • 1
  • 8
  • Please, give an example of your data. What's data2$Pais[11] value when you read using encoding latin1 and utf-8? When you replace the label manually, the error is due to the unknown factor level, not the accent issue. You should first add "México" level to the factor variable and then rename the value – Asayat Sep 24 '13 at 03:52

1 Answers1

2

Try to change the locale in your R session to Spanish, with:

Sys.setlocale("LC_MESSAGES", 'es_MX.UTF-8')

Taken from:

It may be the case that the desired locale is not installed in your computer, in Ubuntu you can use

locale -a

to see which locales are installed. See here for instructions on how to install a locale.

Community
  • 1
  • 1
papirrin
  • 2,004
  • 22
  • 30