r - Combining rows on the x-axis within geom_bar -


i assume asking pretty simple question, not able solve myself. how can create age groups on x-axis in image below. example, 10-20, 20-30, 40-50 etc. within ggplot?

enter image description here

i know create new dataframe, prefer keep worksheet simple , within ggplot. code using:

 figure1 <- ggplot(newdata, aes(x = factor(leeftijd),)) + geom_bar() + xlab("age") +  ylab("loneliness (count)") + ggtitle("overview of distrubtion of lonely people")  figure1 

thanks!

first question didn't ask want answer. i've found better feed dataframe through dplyr , tidyr before goes ggplot.

it makes more readable code , makes whole mess simpler when head around it.

secondly, want:

library(ggplot2) library(tidyr) library(dplyr)   ## created dummy data here, want use own, newdata <- data.frame(as.numeric(round(rnorm(1000,50,10)))) colnames(newdata) <- c("leeftijd")   figure1 <- ggplot(newdata, aes(x = factor(leeftijd),)) + geom_bar() + xlab("age") +    ylab("loneliness (count)") + ggtitle("overview of distrubtion of lonely people")  figure1   ## want, change binwidth whatever you're interested in. newdata %>%   ggplot() +   aes(x=leeftijd) +   geom_histogram(binwidth=10, stat="bin") +   labs(x="age",        y="loneliness (count)",        title="overview of distribution of lonely people") 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -