0

I am trying to overlay two variables on a same figure with ggplot2, so I use melt to get the data in the correct format and then use the following:

Locations <- c("USA","UK","Spain")
vals_1 <- c(44,6,76)
vals_2 <- c(0.2,0.9,4.1)

dat <- data.frame(Locs = Locations,
                  method_1 = vals_1,
                  method_2 = vals_2)
dat2 <- melt(dat,id = "Locs")

ggplot(data = dat2,
       aes(x = Locs, y = value, colour = variable))

but this generates an error. Why does it state that there are no layers?

Is this to do with the class of dat2[,1] and dat2[,2] being a factor? If so, what should it be changed to? I would like the graph to show the string in dat2[,1] on the xaxis and both the variables shown in the plot. Could someone point me in the right direction?

Amend:

After adding

geom_line()

to get

ggplot(data = dat2,
       aes(x = Locs, y = value, colour = variable)) +
  geom_line()

I receive the following error geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

2
  • You haven't added any geom_ to your plot, for example, +geom_bar() Commented Aug 16, 2013 at 8:01
  • thanks, please see amended post Commented Aug 16, 2013 at 8:05

1 Answer 1

2

You have to tell ggplot() which points to connect by lines. This is done by adding group=variable inside the aes().

ggplot(data = dat2,
       aes(x = Locs, y = value, colour = variable,group=variable)) +
  geom_line()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.