• Register
1 vote
120 views

Problem :

I have recently started using R I am very new to R while working on R I am facing the below error whenever I try to create the graph on the ggplot:

“Error: ggplot2 doesn't know how to deal with data of class gg/ggplot”
I often face the error message if I try to create below graphs.
MyCars %>%
ggplot() %>%
geom_bar(mapping = aes(x = Destination))

MyCars %>%
ggplot() %>%
geom_bar(mapping = aes(x = Cost))

MyCars %>%
ggplot() %>%
geom_point(mapping = aes(x = Destination, y = Cost))
I need help in fixing above error.
8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I can help you in fixing your errors. As you are very new to R there are possibilities of mistakes from your end. From my point of view your main mistake is the use of pipe (%>%) instead of using the + . Also I want to suggest you to put your aes() (x\y axis) under your ggplot() and it should not be under your other arguments.

Please refer below example for more clarity:

require(tidyverse)

df <- MyCars

df %>% 
  ggplot(aes(x = factor(vs))) +
  geom_bar()

df %>%
  ggplot(aes(x = mpg, y = disp)) +
  geom_point()
I hope my above solution will help you in fixing the errors.
8 4 1
5,680 points
0 votes

Solution:

The error occurs since of you are attempting to map a numeric vector to data in geom_errorbarGVW[1:64,3]ggplot just performs with data.frame.

Usually, you shouldn't subset within ggplot calls. You are doing so since your standard errors are contained in four separate objects. include them to your original data.frame and you will be able to plot everything in one call.

Here with a dplyr solution to shorten the data and compute the standard error beforehand.

library(dplyr)
d <- GVW %>% group_by(Genotype,variable) %>%
    summarise(mean = mean(value),se = sd(value) / sqrt(n()))

ggplot(d, aes(x = variable, y = mean, fill = Genotype)) + 
  geom_bar(position = position_dodge(), stat = "identity", 
      colour="black", size=.3) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), 
      size=.3, width=.2, position=position_dodge(.9)) +
  xlab("Time") +
  ylab("Weight [g]") +
  scale_fill_hue(name = "Genotype", breaks = c("KO", "WT"), 
      labels = c("Knock-out", "Wild type")) +
  ggtitle("Effect of genotype on weight-gain") +
  scale_y_continuous(breaks = 0:20*4) +
  theme_bw()

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 41 views
0 votes
1 answer 2 views
2 views
Problem: Hello I've been using R for around a year now but only in my spare time outside of work and I've encountered the following error whilst trying to create a graph on ggplot: Error: ggplot2 doesn't know how to deal with data of class gg/ggplot I see the error ... -09-19" ... Any help you can provide on why I'm seeing this error message and how I can correct it would be much appreciated.
asked 5 days ago muktaa 34.6k points
1 vote
1 answer 95 views
95 views
Problem: I am a naive to R. I am trying to create the bar graph using the ggplot2 to compare a type of 2016 primary election and the Bernie Sanders's respective percentage of the votes in that election. I have below code: y = $ Type : Factor w/ 2 levels ... data of class uneval. I have already tried to search the solution on above error around but because of some unknown reasons to me I am stuck.
asked May 27, 2020 Martin K 6.6k points
1 vote
1 answer 63 views
63 views
Problem : I am trying to understand R and I am very new to Programming as wel. If I try to create the box chart with the use of standard error bars I often face below error message: “Error: ggplot2 doesn't know how to deal with data of class numeric”. Looking for help in quick fixing the above error.
asked May 5, 2020 stewart 4k points
1 vote
1 answer 4 views
4 views
Problem: I need help that how to over lay a normal curve : R plot normal distribution with mean and standard deviation.
asked Apr 27 PkGuy 23.5k points
0 votes
1 answer 54 views
54 views
Problem: how i can fix this ? any help ! error: cannot add ggproto objects together. did you forget to add this object to a ggplot object?.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 109 views
109 views
Problem: ggplot error: discrete value supplied to continuous scale.
asked Feb 23 Dan phillip 4.8k points
1 vote
1 answer 3K views
3K views
Problem : I am facing error related to ggproto as following error: cannot add ggproto objects together. did you forget to add this object to a ggplot object?
asked Nov 11, 2019 peterlaw 6.9k points
0 votes
1 answer 13 views
13 views
Problem : I need answer to bellow issue don't know how to automatically pick scale for object of type data.frame. defaulting to continuous.
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 1.7K views
1.7K views
Problem : I need answer to bellow issue don't know how to automatically pick scale for object of type data.frame. defaulting to continuous.
asked Nov 7, 2019 peterlaw 6.9k points