i am unable to create histogram using ggplot

hi. i am new to r studio. i have to create multiple histograms using ggplot.
in my data set i have 17 columns and 1405 rows(all numeric). i have written a code but its giving me wrong output plus an error. please help me in this regard. histogram for All Emotion of sub 001
filee<-data.frame(filee) %>%
dataa <- filee %>%
gather(key="text", value="value") %>%
mutate(text = gsub("\.", " ",text)) %>%
mutate(value = round(as.numeric(value),0)
p<-filee %>%
ggplot(aes_(x=value,color=text,fill=text)) + geom_histogram()+stat_bin(5)+
scale_fill_viridis(discrete=TRUE)+
scale_color_viridis(discrete=TRUE)+
#theme_ipsum()
theme(legend.position = "none",
panel.spacing = unit(0.1,"lines"),
strip.text.x =element_text(size=8)
)+
xlab("")+ ylab("Distribution of Data points of S001")+
facet_wrap(~text)
error message:
Error in aes_(x = value, color = text, fill = text) : object 'value' not found

thanks in advance

The data frame that contains value variable is called dataa not filee, if this doesn't solve your issue, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

thats for prompt response.
but error is still same.
my dataset is very simple.
17 colums(h_226,h_813..) All the colums have different length of data values with minimum range is 53 and maximum is 1405.
by using gather function i have converted them to two colums. text-> with all the headers of colums till and value-> original data values.

i dont know how to create that reprex thats why i tried to explain in description.
waiting for your response.

Now i am receiving this error restarting.

Error in eval(lhs, parent, parent) : object 'dataa' not found

Read this guide and try to make one

i have created reprex but its in html format. how do i share it with you as its not allowed to upload any attachment being a new user.?.

Please follow the guide, there is no need to upload any file, just to post the output as is.

squad<-tibble::tribble( ~S001_Hpy_226, ~S001_Hpy_365, ~S001_Hpy_813, ~S001_Sad_292, ~S001_Sad_293, ~S001_Sad_296, ~S001_Ang_278, 0.158239735, -0.800922515, -0.752463016, -0.93976707, -0.001246463, 0.491402485, 0.073815246, -0.72434794, -1, -0.648522809, -0.93976707, -0.023306738, 0.491402485, -0.68703515, 0.089073079, -0.91459096, -0.563822428, -0.93976707, -0.10638315, 0.462683871, -0.77005166, -0.3024, -0.843520101, -0.460712104, -0.93976707, -0.062006546, -0.537315191, -0.93816113, -0.693858246, -0.67333103, -0.451360743, -0.93976707, -0.056470619, -0.960095522, -0.846116383, -0.862108287, -0.687492995, -0.404659328, -0.93976707, 0.064701532, -1, -0.73978531, -0.907549161, -0.645018216, -0.246558809, -0.875028736, 0.124850672, -0.960095522, -0.574907058, -0.702547239, -0.616720207, -0.181762511, -1, 0.031770998, -0.799779713, -0.582344331, -0.763875498, -0.503675749, -0.218767428, -0.945999821, 0.070181552, -0.87182271, 0.261077799, -0.702547239, -0.602576747, -0.107923999, -0.9174729, -0.001246463, -0.80454135, NA, -1, -0.362700502, -0.135586765, -0.960333894, 0.042757468, -0.161680614, NA, -0.953507045, -0.404954688, -0.163281518, -0.858632813, -0.045406161, 0.131739946, NA, -0.930462372, -0.687492995, -0.061890168, -0.756931732, 0.037265439, -0.072512418, NA, -0.866629603, -0.687492995, 0.048232795, -0.692259666, 0.031770998, -0.182190415, NA, -0.817167359, -0.687492995, 0.057387036, -0.79134374, -0.028827915, -0.084570736, NA, -0.844072846, -0.531914773, 0.08482896, NA, 0.048247088, -0.248453764, NA, -0.826116337, -0.645018216, 0.130496397, NA, -0.001246463, -0.157589716, NA, -0.715614182, -0.560168509, 0.15785557, NA, -0.017788007, -0.024588741, NA, -0.659278348, -0.517793423, 0.15785557, NA, 0.009769058, -0.048489138, NA ) head(squad) #> # A tibble: 6 x 7 #> S001_Hpy_226 S001_Hpy_365 S001_Hpy_813 S001_Sad_292 S001_Sad_293 S001_Sad_296 #> #> 1 0.158 -0.801 -0.752 -0.940 -0.00125 0.491 #> 2 -0.724 -1 -0.649 -0.940 -0.0233 0.491 #> 3 0.0891 -0.915 -0.564 -0.940 -0.106 0.463 #> 4 -0.302 -0.844 -0.461 -0.940 -0.0620 -0.537 #> 5 -0.694 -0.673 -0.451 -0.940 -0.0565 -0.960 #> 6 -0.862 -0.687 -0.405 -0.940 0.0647 -1 #> # … with 1 more variable: S001_Ang_278

You are just providing sample data but not your code, this would be a proper reproducible example

library(tidyverse)
library(viridis)

squad <- data.frame(
  S001_Hpy_226 = c(0.158239735,-0.72434794,0.089073079,
                   -0.3024,-0.693858246,-0.862108287,-0.907549161,
                   -0.702547239,-0.763875498,-0.702547239,-1,-0.953507045,
                   -0.930462372,-0.866629603,-0.817167359,-0.844072846,-0.826116337,
                   -0.715614182,-0.659278348),
  S001_Hpy_365 = c(-0.800922515,-1,-0.91459096,
                   -0.843520101,-0.67333103,-0.687492995,-0.645018216,-0.616720207,
                   -0.503675749,-0.602576747,-0.362700502,-0.404954688,
                   -0.687492995,-0.687492995,-0.687492995,-0.531914773,
                   -0.645018216,-0.560168509,-0.517793423),
  S001_Hpy_813 = c(-0.752463016,-0.648522809,
                   -0.563822428,-0.460712104,-0.451360743,-0.404659328,-0.246558809,
                   -0.181762511,-0.218767428,-0.107923999,-0.135586765,
                   -0.163281518,-0.061890168,0.048232795,0.057387036,0.08482896,
                   0.130496397,0.15785557,0.15785557),
  S001_Sad_292 = c(-0.93976707,-0.93976707,-0.93976707,
                   -0.93976707,-0.93976707,-0.93976707,-0.875028736,-1,
                   -0.945999821,-0.9174729,-0.960333894,-0.858632813,
                   -0.756931732,-0.692259666,-0.79134374,NA,NA,NA,NA),
  S001_Sad_293 = c(-0.001246463,-0.023306738,-0.10638315,
                   -0.062006546,-0.056470619,0.064701532,0.124850672,
                   0.031770998,0.070181552,-0.001246463,0.042757468,-0.045406161,
                   0.037265439,0.031770998,-0.028827915,0.048247088,
                   -0.001246463,-0.017788007,0.009769058),
  S001_Sad_296 = c(0.491402485,0.491402485,0.462683871,
                   -0.537315191,-0.960095522,-1,-0.960095522,-0.799779713,
                   -0.87182271,-0.80454135,-0.161680614,0.131739946,
                   -0.072512418,-0.182190415,-0.084570736,-0.248453764,-0.157589716,
                   -0.024588741,-0.048489138),
  S001_Ang_278 = c(0.073815246,-0.68703515,-0.77005166,
                   -0.93816113,-0.846116383,-0.73978531,-0.574907058,
                   -0.582344331,0.261077799,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
)

squad %>%
    gather(key = "text", value = "value") %>% 
    ggplot(aes(x = value, color = text, fill = text)) +
    geom_histogram() +
    scale_fill_viridis(discrete = TRUE) +
    scale_color_viridis(discrete = TRUE) +
    theme(legend.position = "none",
          panel.spacing = unit(0.1,"lines"),
          strip.text.x =element_text(size=8)
    ) +
    xlab("") + 
    ylab("Distribution of Data points of S001") +
    facet_wrap(~text)

Created on 2020-03-26 by the reprex package (v0.3.0.9001)

Is this what you want to do?

2 Likes

Thanks alott.. this is exactly what i was trying to do.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.