r - ggplot2: Error: Discrete value supplied to continuous scale -


for sample dataframe:

df1 <- structure(list(country.name = structure(c(4l, 11l, 10l, 2l, 1l,         3l, 8l, 5l, 7l, 9l, 6l), .label = c("austria", "belgium", "czech republic",         "denmark", "france", "germany", "netherlands", "norway", "poland",         "sweden", "switzerland"), class = "factor"), level = c(2l, 2l,         2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l), no.regions = c(5l, 7l, 8l,         11l, 9l, 8l, 7l, 16l, 12l, 15l, 14l), min_result = c(42.59, 33.57,         43.1, 38.46, 41.76, 44.05, 41.67, 36.32, 36.18, 42.79, 39.91),         max_result = c(50.24, 46.56, 58.24, 57.41, 61.07, 64.56,         63.25, 58.19, 59.14, 69.19, 67.11), diff = c(7.65, 12.99,         15.14, 18.95, 19.31, 20.51, 21.58, 21.87, 22.96, 26.4, 27.2        ), rd = c(-0.07, 0.131, -0.091, -0.153, -0.172, 0.203, -0.166,         0.145, -0.228, -0.266, -0.261), rdci_lower = c(-0.21, -0.028, -0.194, -0.328, -0.376, 0.076, -0.315, 0.075, -0.407, -0.348,         -0.347), rdci_upper = c(0.07, 0.29, 0.012, 0.021, 0.031,        0.331, -0.017, 0.216, -0.049, -0.184, -0.175), rdpvalue = c(0.3237,        0.1113, 0.08, 0.0829, 0.1017, 0.0023, 0.0299, 0, 0.0149,        0, 0), diff_order = structure(1:11, .label = c("denmark",         "switzerland", "sweden", "belgium", "austria", "czech republic",        "norway", "france", "netherlands", "poland", "germany"), class = "factor", scores = structure(c(19.31,        18.95, 20.51, 7.65, 21.87, 27.2, 22.96, 21.58, 26.4, 15.14,        12.99), .dim = 11l, .dimnames = list(c("austria", "belgium",        "czech republic", "denmark", "france", "germany", "netherlands",        "norway", "poland", "sweden", "switzerland"))))), .names = c("country.name",        "level", "no.regions", "min_result", "max_result", "diff", "rd",        "rdci_lower", "rdci_upper", "rdpvalue", "diff_order"), row.names = c(na,        -11l), class = "data.frame") 

i trying create plot using ggplot2:

library(ggplot2)  df1$diff_order <- reorder(df1$country.name, df1$diff) #set order of countries diff (if not already), , set plot   <- ggplot(df1, aes((x=diff_order), y=country.name,fill=level)) +   geom_bar(stat="identity") +   xlab("country") +   theme_classic() +   coord_flip() +   scale_fill_manual(values=c("#009e73", "#0072b2"),name="nuts level") +   ylim(0, 40) +   ggtitle("all")  +   theme(plot.title = element_text(hjust = 0)) +   theme(axis.title.x = element_blank()) +   theme(axis.text=element_text(size=6),         axis.title=element_text(size=10,face="bold"))  

however, error:

error: discrete value supplied continuous scale 

i know error has been extensively reported elsewhere, can't find adequate solution problem. understand it, r having trouble 1 of scales, can try change class (e.g. df1$country.name <- as.character(df1$country.name) df1$level <- as.factor(df1$level)), , still same error.

does have ideas?

update

if change code @mlavoie suggested...

a <- ggplot(df1, aes((x=diff_order), y=country.name,fill=as.factor(level))) +   geom_bar(stat="identity") +   xlab("country") +   theme_classic() +   coord_flip() +   scale_fill_manual(values=c("#009e73", "#0072b2"),name="nuts level") +   ggtitle("all")  +   theme(plot.title = element_text(hjust = 0)) +   theme(axis.title.x = element_blank()) +   theme(axis.text=element_text(size=6),         axis.title=element_text(size=10,face="bold")) 

... following error:

error in geom_bar(stat = "identity") + xlab("country") :    non-numeric argument binary operator 

what doing wrong?

i can produce graph far, want countries (in order) on y axes , 'diff' on x axis. ideas?

enter image description here

finally, managed make work... thank gave me comments:

df1$diff_order <- reorder(df1$country.name, df1$diff) #set order of countries diff, , set plot   <- ggplot(df1, aes((x=diff_order), y=diff,fill=as.factor(level))) +   geom_bar(stat="identity") +   xlab("country") +   theme_classic() +   coord_flip() +   scale_fill_manual(values=c("#009e73", "#0072b2"),name="nuts level") +   ggtitle("all")  +   theme(plot.title = element_text(hjust = 0)) +   theme(axis.title.x = element_blank()) +   theme(axis.text=element_text(size=9),         axis.title=element_text(size=10,face="bold"))  

enter image description here


Comments