ggplot multiple grouping bar - r
I would like to know how to get 9 grouping bar plot (3x3) together.
My CSV:
data <- read.csv("http://pastebin.com/raw.php?i=6pArn8GL", sep = ";")
The 9 plots should be grouped according "Type" A to I.
Then each grouped bar plot should have the frequency on the y axis, the x axis is grouped by 1 pce to 6 pce and subdivided by year.
I have the following example on Excel (cf. image) and would like to create the same result on r with ggplot. Is it possible?
First, reshape your data from wide to long format.
library(reshape2)
df.long<-melt(df,id.vars=c("ID","Type","Annee"))
Next, as during importing data letter X is added to variable names starting with number, remove it with substring().
df.long$variable<-substring(df.long$variable,2)
Now use variable as x, value as y, Annee for fill and geom_bar() to get barplot. With facet_wrap() you can split data by Type.
ggplot(df.long,aes(variable,value,fill=as.factor(Annee)))+
geom_bar(position="dodge",stat="identity")+
facet_wrap(~Type,nrow=3)
Using #Didzis reshaped data , here a lattice version:
barchart(value~variable|Type,
groups=Annee,data=df.long,layout=c(3,3),
between=list(3,3),
axis=axis.grid,
auto.key=TRUE)
Related
How do I make my row names appear on my x axis? And the numbers on from my variables appear as the y axis?
I created a dataframe with countries as row names and percentages as obs. from the variables, but when making a histogram it seems that the percentages from the variables are occupying the x axis and the country names aren't even there. How do I make it so that the countrie's names are on the x axis and the variables on the y? Country <- c('Albania','Armenia','Austria','Belarus','Belgium','Bosnia and Herzegovina','Bulgaria','Croatia','Cyprus','Czechia','Denmark','Estonia','Finland','France','Georgia','Germany','Greece','Hungary','Iceland','Ireland','Italy','Latvia','Lithuania','Luxembourg','Malta','Moldova','Montenegro','Netherlands','Norway','Poland','Portugal','Romania','Russia','Serbia','Slovakia','Slovenia','Spain','Sweden','Switzerland','Turkey','Ukraine','United Kingdom') Anxiety.Disorders <- c(3.38,2.73,5.22,3.03,4.92,3.70,3.84,3.74,5.61,3.59,5.18,3.01,3.59,6.37,2.46,6.37,5.58,3.69,5.15,5.66,5.57,3.04,3.06,5.19,5.14,2.77,3.55,6.43,7.33,3.68,5.52,3.41,3.02,3.60,3.61,3.60,5.14,5.16,5.28,3.85,3.09,4.43) Depressive.Disorders <- c(2.42,3.16,3.66,4.84,4.35,2.88,3.30,3.60,3.88,3.25,3.62,4.78,5.08,4.55,2.98,4.42,4.56,3.53,3.55,4.37,3.94,4.44,5.20,3.95,3.69,3.77,2.96,4.34,3.95,2.72,5.27,2.88,4.36,3.15,2.87,3.58,3.91,4.84,4.17,3.76,5.02,4.35) Bipolar.Disorder <- c(0.72,0.77,0.95,0.73,0.91,0.79,0.67,0.77,1.04,0.75,0.99,0.71,0.99,0.93,0.67,0.79,0.93,0.74,0.97,0.80,0.95,0.71,0.73,0.95,0.97,0.67,0.74,0.94,0.85,0.76,0.97,0.78,0.70,0.74,0.76,0.75,0.97,1.04,0.98,0.85,0.73,1.05) G08 <- data.frame(Country, Anxiety.Disorders, Depressive.Disorders, Bipolar.Disorder) row.names(G08) <- G08$Country G08[1] <- NULL hist(G08$Anxiety.Disorders)
I use the melt() call to create one observation per row. Then, I use ggplot to produce the bar plot. library(ggplot2) library(reshape2) Country <- c('Albania','Armenia','Austria','Belarus','Belgium','Bosnia-Herzegovina','Bulgaria','Croatia','Cyprus','Czechia','Denmark','Estonia','Finland','France','Georgia','Germany','Greece','Hungary','Iceland','Ireland','Italy','Latvia','Lithuania','Luxembourg','Malta','Moldova','Montenegro','Netherlands','Norway','Poland','Portugal','Romania','Russia','Serbia','Slovakia','Slovenia','Spain','Sweden','Switzerland','Turkey','Ukraine','United Kingdom') Anxiety.Disorders <- c(3.38,2.73,5.22,3.03,4.92,3.70,3.84,3.74,5.61,3.59,5.18,3.01,3.59,6.37,2.46,6.37,5.58,3.69,5.15,5.66,5.57,3.04,3.06,5.19,5.14,2.77,3.55,6.43,7.33,3.68,5.52,3.41,3.02,3.60,3.61,3.60,5.14,5.16,5.28,3.85,3.09,4.43) Depressive.Disorders <- c(2.42,3.16,3.66,4.84,4.35,2.88,3.30,3.60,3.88,3.25,3.62,4.78,5.08,4.55,2.98,4.42,4.56,3.53,3.55,4.37,3.94,4.44,5.20,3.95,3.69,3.77,2.96,4.34,3.95,2.72,5.27,2.88,4.36,3.15,2.87,3.58,3.91,4.84,4.17,3.76,5.02,4.35) Bipolar.Disorder <- c(0.72,0.77,0.95,0.73,0.91,0.79,0.67,0.77,1.04,0.75,0.99,0.71,0.99,0.93,0.67,0.79,0.93,0.74,0.97,0.80,0.95,0.71,0.73,0.95,0.97,0.67,0.74,0.94,0.85,0.76,0.97,0.78,0.70,0.74,0.76,0.75,0.97,1.04,0.98,0.85,0.73,1.05) G08 <- data.frame(Country, Anxiety.Disorders, Depressive.Disorders, Bipolar.Disorder) G08melt <- melt(G08, "Country") G08.bar <- ggplot(G08melt, aes(x = Country, y=value)) + geom_bar(aes(fill=variable),stat="identity", position ="dodge") + theme_bw()+ theme(axis.text.x = element_text(angle=-40, hjust=.1)) G08.bar
Looking at your question, I think you tried to do a grouped column diagram instead of a histogram. You can do the plot directly using the barplot function from the graphics package. But before that, you need to convert your dataframe into a matrix. I removed the first column from G08. mat<-G08[,-1] Now just simply use the barplot function on the transpose of the matrix mat and use the names parameter of barplot to write the names of the Countries on the x-axis: barplot(t(mat),beside=T,col=c('red','blue','gold'),border=NA,names=G08$Country,cex.names=0.45,las=2) par(new=T) legend('topright',c("Anxiety","Depressive","Bipolar"),fill=c("red","blue","gold"),cex=0.5,title='Disorder types') Suggestion: For a little bit of more 'fresh air' in the graph, you can just set beside=F in barplot and get a stacked column diagram:
Graphing different variables in the same graph R- ggplot2
I have several datasets and my end goal is to do a graph out of them, with each line representing the yearly variation for the given information. I finally joined and combined my data (as it was in a per month structure) into a table that just contains the yearly means for each item I want to graph (column depicting year and subsequent rows depicting yearly variation for 4 different elements) I have one factor that is the year and 4 different variables that read yearly variations, thus I would like to graph them on the same space. I had the idea to joint the 4 columns into one by factor (collapse into one observation per row and the year or factor in the subsequent row) but seem unable to do that. My thought is that this would give a structure to my y axis. Would like some advise, and to know if my approach to the problem is effective. I am trying ggplot2 but does not seem to work without a defined (or a pre defined range) y axis. Thanks
I would suggest next approach. You have to reshape your data from wide to long as next example. In that way is possible to see all variables. As no data is provided, this solution is sketched using dummy data. Also, you can change lines to other geom you want like points: library(tidyverse) set.seed(123) #Data df <- data.frame(year=1990:2000, v1=rnorm(11,2,1), v2=rnorm(11,3,2), v3=rnorm(11,4,1), v4=rnorm(11,5,2)) #Plot df %>% pivot_longer(-year) %>% ggplot(aes(x=factor(year),y=value,group=name,color=name))+ geom_line()+ theme_bw() Output:
We could use melt from reshape2 without loading multiple other packages library(reshape2) library(ggplot2) ggplot(melt(df, id.var = 'year'), aes(x = factor(year), y = value, group = variable, color = variable)) + geom_line() -output plot Or with matplot from base R matplot(as.matrix(df[-1]), type = 'l', xaxt = 'n') data set.seed(123) df <- data.frame(year=1990:2000, v1=rnorm(11,2,1), v2=rnorm(11,3,2), v3=rnorm(11,4,1), v4=rnorm(11,5,2))
How to plot bar charts with 'n' number of columns and group by another column?
I am learning r currently and I have an r data-frame containing data I have scraped from a football website. There are 58 columns(Variables,attributes) for each row. Out of these variables, I wish to plot 3 in a single bar chart.I have 3 important variables 'Name', 'Goals.with.right.foot', 'Goals.with.left.foot'. What I want to build is a bar chart with each 'Name' appearing on the x-axis and 2 independent bars representing the other 2 variables. Sample row entry: {......., RONALDO, 10(left), 5(right),............} I have tried playing around a lot with ggplot2 geom_bar with no success. I have also searched for similar questions however I cannot understand the answers. Is anyone able to explain simply how do I solve this problem? my data frame is called 'Forwards' who are the strikers in a game of football. They have attributes Name, Goals.with.left.foot and Goals.with.right.foot. barplot(counts, main="Goals", xlab="Goals", col=c("darkblue","red"), legend = rownames(counts))
You could try it this way: I simulated a frame as a stand in for yours, just replace it with a frame containing the columns you're interested in: df <- data.frame(names = letters[1:5], r.foot = runif(5,1,10), l.foot = runif(5,1,10)) # transform your df to long format library(reshape2) plotDf <- melt(df, variable.name = 'footing', value.name = 'goals') # plot it library(ggplot2) ggplot(plotDf, aes(x = names, y = goals, group = footing, fill = footing)) + geom_col(position = position_dodge()) #does the same as geom_bar, but uses stat_identity instead of stat_count Results in this plot: your plot This works, because ggplot expects one variable containing the values needed for the y-axis and one or more variable containing the grouping factor(s). with the melt-function, your data.frame is merged into the so called 'long format' which is exactly the needed orientation of data.
Creating boxplot in same graph and scale with two different length of dataset
I have a two set of data with different length. Sameple datatype is: A=c(423,430,500,460,457,300,325,498,450,453,486,459) B=c(300,325,356345,378,391,367) I want to create boxplot for them within a same graph and same scale. I tried it in ggplot2 in R. I also tried default boxplot in R. boxplot (A~B) but it showed error. I would like to use ggplot2 in R.
You have to create a dataset with those 2 vectors and then plot. library(ggplot2) A=c(423,430,500,460,457,300,325,498,450,453,486,459) B=c(300,325,356345,378,391,367) # create a dataset for each vector df_A = data.frame(value=A, id="A") df_B = data.frame(value=B, id="B") # combine datasets df = rbind(df_A, df_B) # create the box plot ggplot(df, aes(id, value)) + geom_boxplot()
How do I put multiple boxplots in the same graph in R?
Sorry I don't have example code for this question. All I want to know is if it is possible to create multiple side-by-side boxplots in R representing different columns/variables within my data frame. Each boxplot would also only represent a single variable--I would like to set the y-scale to a range of (0,6). If this isn't possible, how can I use something like the panel option in ggplot2 if I only want to create a boxplot using a single variable? Thanks! Ideally, I want something like the image below but without factor grouping like in ggplot2. Again, each boxplot would represent completely separate and single columns.
ggplot2 requires that your data to be plotted on the y-axis are all in one column. Here is an example: set.seed(1) df <- data.frame( value = runif(810,0,6), group = 1:9 ) df library(ggplot2) ggplot(df, aes(factor(group), value)) + geom_boxplot() + coord_cartesian(ylim = c(0,6) The ylim(0,6) sets the y-axis to be between 0 and 6 If your data are in columns, you can get them into the longform using melt from reshape2 or gather from tidyr. (other methods also available).
You can do this if you reshape your data into long format ## Some sample data dat <- data.frame(a=rnorm(100), b=rnorm(100), c=rnorm(100)) ## Reshape data wide -> long library(reshape2) long <- melt(dat) plot(value ~ variable, data=long)