Incorrect graph legend - mix up factorial and numeric variables

I have a database with several columns with different species names, and one with a range of years. I have to produce a graph between species and years of sightings: everything works fine and the graph comes up, but as a legend it doesn't write the name in words but in numbers. For example, instead of putting "Citizen Science"=red, it writes 1=red. To do this I use a function that has already been tested on other PCs and everything works correctly, while on mine it doesn't.

I noticed that on other PCs it reads the variables as characters while on mine it gives them as factors. I tried modifying them with stringsAsFactors=F and indeed they all come out as character, but doing so completely throws the graph off. I can't understand what the problem is and why only my computer reads the dataframe this way.

If it is helpful, this is the plot part of the function:


plot(rbind(c(start-2,0),c(end,length(species))),type="n",ylab="Species richness",xlab="Years of record")

Without tipidati, this problem would have to be reverse-engineered. What would be helpful: a reprex. See the FAQ.

The issue is likely related to the fact that your computer is reading the variables as factors while others are reading them as characters. Factors in R are integer values that represent the levels of a categorical variable. When plotting, R uses the integer values instead of the character labels. To fix this, you can use the as.character() function to convert the factors to characters before plotting.

You can modify your code as following:

Copy code


This will convert tipidati to a character vector and should display the correct labels in the legend. It's also important to check that the data is loaded the same way in all computers, maybe there is some difference in reading the data that is causing this issue.

1 Like

Thank you for your reply. Unfortunately, it didn't solve the problem, I have tried both converting typed data to character with your string and also reconverting my database, but the result doesn't change. I tried loading the csv in two different ways (file.choose with read. table, and read.csv), moreover I use the same one I tried on other computers (read.csv). I really have no idea what the problem might be.

1 Like

Try to read the .csv with tidyverse.

1 Like

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide