0

I am working on a dataset with multiple variables as seen on sample below;(actual data set contains:84 obs. 24 variables). I want to create a single plot which takes in all the variables as opposed to creating a single plot for each variable.

Fruit    Vitamin A(mg) Vitamin C(mg) Calcium(mg)
Pear         61            8             11
Apple        10            2             3
Cherry       35            10            11
Fig          5             2             67

I have tried the code below, an altered version of one suggested in one of the forums;

library(ggplot2)
g<- ggplot(FR, aes(Fruit)
g + geom_point() + facet_grid(. ~ FR[2:26,])

I get error;

Error: unexpected symbol in: "g<- ggplot(FR, aes(Fruit) g"

I am open to any better suggestions for alternatives to represent the dataset.

1
  • geom_point() needs both a y and and and x axis. You have only specified an x-axis. Do you want the other columns to be the Y? Commented May 4, 2016 at 19:05

3 Answers 3

2

How about this:

enter image description here

To do this, you need to reshape your dataset using gather{tidyr}. Here is a reproducible example on how to do this:

# load libraries
  library(ggplot2)
  library(ggthemes)
  library(tidyr)
  library(googleVis)

# get data for a reproducible example
  data("Fruits")
  colnames(Fruits)[4] <- "Vitamin A(mg)"
  colnames(Fruits)[5] <- "Vitamin C(mg)"
  colnames(Fruits)[6] <- "Calcium(mg)"
  Fruits <- Fruits[ c("Fruit","Vitamin A (mg)" , "Vitamin C (mg)", "Calcium (mg)")]

# reshape the dataset
  df <- gather(data=Fruits, key=Fruit)
  colnames(df)[2] <- "vitamin"


# Plot !
  ggplot(data=df) +
    geom_point(aes(x=vitamin, y=value , color=vitamin)) +
    facet_grid(Fruit~., scale="free_x") +
    theme_minimal() 
Sign up to request clarification or add additional context in comments.

1 Comment

This is closest to what I wanted, I made fruits to be my x-values. While the plot looks great on small data set, it's all clustered up and blurry on a data set as large as mine, I might need to categories the variables. Thank you, this was helpful.
1

I believe you're missing a closing parenthesis. Change:

g<- ggplot(FR, aes(Fruit)

to

g<- ggplot(FR, aes(Fruit))

In my experience, "unexpected symbol" errors usually mean you forgot to close parentheses or braces.

1 Comment

I got error: Error in layout_base(data, cols, drop = drop) : At least one layer must contain all variables used for facetting
0

You aren't specifying the axes enough.

ggplot(FR, aes(x = Fruit, y = Vitamin A(mg), 
    shape = as.factor(Fruit), 
    color = as.factor(Fruit))) + 
    geom_point() +
    geom_point(aes(x = Fruit, y = Vitamin C(mg))) +
    geom_point(aes(x = Fruit, y = Calcium(mg)))

Is that what you wanted?

1 Comment

Works great for variables with the same unit, but not for my case where I have in my data Calories in (kcal). Sorry I forgot to mention this earlier.

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.