I get a repeated error message "Discrete value supplied to continuous scale
" even though I'm using a numeric variable.
Here is my code
name = c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel",)
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- tibble(name, year, portion)
ggplot(df) geom_line(aes(x = year, y = portion, group = name), colour="black") geom_text(aes(year == 1999, label = name), hjust = -.1)
My year
variable is already a numeric value, so I don't need to convert it? What I am missing?
CodePudding user response:
The problem is the geom_text line. Presumably you want to show each name at the corresponding point so put the x and y aes values in ggplot to share them with both geom_line and geom_text and then we can just specify the aesthetics specific to each.
library(ggplot2)
ggplot(df, aes(x = year, y = portion))
geom_line(aes(group = name), colour = "black")
geom_text(aes(label = name), hjust = -0.1)
or if the intention was to only label the last point then:
library(dplyr)
library(ggplot2)
ggplot(df, aes(x = year, y = portion))
geom_line(aes(group = name), colour = "black")
geom_text(aes(label = name), data = slice_tail, hjust = -0.1)
Depending on what you want slice_tail can be replaced with one of these:
~ slice_max(., year) # all rows having the largest year
~ filter(., year == 1999) # all rows having year 1999
~ group_by(., name) %>% slice_max(year) %>% ungroup # row of max year in each name
Note
The input shown in the question has errors in it so we used this:
name = c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel")
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- data.frame(name, year, portion)
CodePudding user response:
Both X and Y should be numeric in this case maybe? Is also Y numeric. If you say that you solved that the problem should rely on the ggplot() function or some layer.
By running your code I see that that problem should be in the geom_text() layer. Without it we get the plot fine. If you want to get the names in the specific points this worked for me (although I am not sure if that is what you are intending!:
name <- c("Alberto Manguel", "Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel","Alberto Manguel")
year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999)
portion = c(0.0044117647, 0.0191846523, 0.0151898734, 0.0042075736, 0.0044493882, 0.0021881838, 0.0146396396, 0.0162689805, 0.0136054422, 0.0158730159)
df <- data.frame(name, year, portion)
plot <- ggplot() geom_line(aes(x = year, y = portion, group = name), colour="black")
geom_text(aes(x=year, y=portion, label=name))
It was probably giving error for the fact that it took NAME as a discrete variable so it cannot really be used as variable itself but only as label?