1

For a 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 am trying to create a plot using ggplot2:

library(ggplot2) df1$diff_order <- reorder(df1$country.name, df1$diff) #Set order of countries by diff (if they are not already), and then set this to be plot a <- 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")) a 

However, I get the error:

Error: Discrete value supplied to continuous scale

I know this error has been extensively reported elsewhere, however, I can't find an adequate solution to my problem. As I understand it, R is having trouble with one of my scales, however, I can try to change their class (e.g. df1$country.name <- as.character(df1$country.name) df1$level <- as.factor(df1$level)), and I still get the same error.

Does anyone have any ideas?

Update

If I change the code as @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")) 

I get the following error:

Error in geom_bar(stat = "identity") + xlab("Country") : non-numeric argument to binary operator

What am I doing wrong?

I can produce this graph so far, but I want the countries (in order) only on the y-axes and 'diff' on the x-axis. Any ideas?

enter image description here

6
  • 1
    using fill=as.factor(level)) and removing ylim(0, 40) should fix your problem Commented Apr 6, 2016 at 13:43
  • Thanks @MLavoie. I tried what you suggested, but still get a (different) error - what am I doing wrong? Commented Apr 6, 2016 at 14:02
  • 1
    You have one spurious ) at the end of ggplot(df1, aes((x=diff_order), y=country.name,fill=as.factor(level)))) Commented Apr 6, 2016 at 14:12
  • Thanks, @bnord. This prints the graph, but the countries are not in order and I haven't got my axis of choice (diff) on the x axis (only countries). When I try to set limits on the y axis, I get the original error... Commented Apr 6, 2016 at 14:29
  • Do you maybe actually want ggplot(df1, aes(x=diff, y=country.name,fill=as.factor(level)))? Commented Apr 6, 2016 at 14:34

1 Answer 1

2

Finally, I managed to make it work... so thank you to those who gave me comments:

df1$diff_order <- reorder(df1$country.name, df1$diff) #Set order of countries by diff, and then set this to be plot a <- 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")) a 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.