How to bar plot answers per category in ggplot? - r

I have a tibble created like this:
tibble(district = c(1, 5, 3, 5, 2, 7, 8, 1, 1, 2, 2, 4, 5, 6, 8, 6, 3),
housing = c(1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 3, 2, 1, 1, 1, 3, 2))
Now I would like to know how the type of housing is distributed per district. Since the amount of respondents per district is different, I would like to work with percentages. Basically I'm looking for two plots;
1) One barplot in which the percentage of housing categories is visualized in 1 bar per district (since it is percentages all the bars would be of equal height).
2) A pie chart for every district, with the percentage of housing categories for that specific district.
I am however unable to group the data is the wished way, let along compute percentages of them. How to make those plots?
Thanks ahead!

Give this a shot:
library(tidyverse)
library(ggplot2)
# original data
df <- data.frame(district = c(1, 5, 3, 5, 2, 7, 8, 1, 1, 2, 2, 4, 5, 6, 8, 6, 3),
housing = c(1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 3, 2, 1, 1, 1, 3, 2))
# group by district
df <- df %>%
group_by(district) %>%
summarise(housing=sum(housing))
# make percentages
df <- df %>%
mutate(housing_percentage=housing/sum(df$housing)) %>%
mutate(district=as.character(district)) %>%
mutate(housing_percentage=round(housing_percentage,2))
# bar graph
ggplot(data=df) +
geom_col(aes(x=district, y=housing_percentage))
# pie chart
ggplot(data=df, aes(x='',y=housing_percentage, fill=district)) +
geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar("y", start = 0) +
theme_void()
Which yields the following plots:

Related

Plot histogram for each group dplyr

Let's cosnider very easy dataframe containing four groups:
cat <- c(1, 0, 0, 1, 2, 1, 2, 3, 2, 1, 3)
var <- c(10, 5, 3, 2, 5, 1, 2, 10, 50, 2, 30)
df <- data.frame(cat, var)
What I would like to do is that using dplyr plot distribution of values between those four categories
I have the feeling that it can be eaisly done with group_by, but I'm not sure how it can be done. Do you know how I can do it?

Is there a way, i can order the axis on a melted ggplot? [duplicate]

This question already has answers here:
Order discrete x scale by frequency/value
(7 answers)
How do you specifically order ggplot2 x axis instead of alphabetical order? [duplicate]
(2 answers)
ggplot2, Ordering y axis
(1 answer)
R ggplot ordering bars within groups
(1 answer)
Closed 6 months ago.
I have a Problem with a Plot I want to order, but it seems like it cant be.
install.packages("reshape2")
library(reshape2)
install.packages("ggplot2")
library(ggplot2)
df <- createRegressionTable(data,colname)
gg <- melt(df, id = "colname")
return(
ggplot(gg, aes(
x = colname, y = variable, fill = value
)) +
geom_tile(show.legend = FALSE) +
geom_text(aes(label = value), alpha = 0.6) +
scale_fill_gradient(low = "#D5E8D4", high = "#F8CECC") +
labs(
x = "Regressant",
y = "Regressor"
) +
theme(legend.key = element_blank())
)
I know the function createRegressionTable is a black box but this is the result:
list(colname = c("zielrichtungU", "zielrichtungO",
"imitationU", "imitationO", "steuerungU", "steuerungO", "neuheitU",
"neuheitO", "netzwerkU", "netzwerkO"), zielrichtungU = c(5, 1,
5, 1, 3, 4, 1, 1, 1, 1), zielrichtungO = c(1, 5, 1, 5, 1, 5,
3, 5, 1, 1), imitationU = c(5, 1, 5, 5, 1, 5, 1, 1, 4, 1), imitationO = c(1,
5, 5, 5, 1, 1, 5, 5, 5, 5), steuerungU = c(3, 1, 1, 1, 5, 5,
1, 2, 1, 1), steuerungO = c(4, 5, 5, 1, 5, 5, 3, 5, 1, 3), neuheitU = c(1,
3, 1, 5, 1, 3, 5, 5, 1, 1), neuheitO = c(1, 5, 1, 5, 2, 5, 5,
5, 1, 1), netzwerkU = c(1, 1, 4, 5, 1, 1, 1, 1, 5, 5), netzwerkO = c(1,
1, 1, 5, 1, 3, 1, 1, 5, 5))
I tested whether the output of melt is scrambled, but it seems to be ordered, as I wished, and now I don't know where the problem lies
And here is the Plot, that I'd love to order:

R: ggplot multiple bars within each group

I want to have a barplot using ggplot2 that display multiple bars within each group, but in my plot, I have 4 bars instead of 8 for each group. I will appreciate your help.
here is my code:
levels = c('D', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9')
method = c('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7','G8')
ave = c(4, 4, 4, 4, 5, 1, 2, 6, 3, 5, 2, 2, 2, 2, 5, 3, 4, 1, 1, 1, 2,
2, 2, 2, 3, 3, 2, 1, 1, 1, 1, 3, 4, 5, 6, 8, 9, 7, 1, 2, 3, 3, 4, 5, 7,
6, 1, 1, 1, 2, 5, 7, 7, 8, 9, 1, 4, 6, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
levels = factor(c(rep(levels,8)))
method = factor(c(rep(method,10)))
dat = data.frame(levels,ave,method)
dodge = position_dodge(width = .9)
p = ggplot(dat,mapping =aes(x = as.factor(levels),y = ave,fill =
as.factor(method)))
p + geom_bar(stat = "identity",position = "dodge") +
xlab("levels") + ylab("Mean")
It looks like geom_bar will only plot bars for observations that exist; if you want to have bars for every method (assuming you want each level to have a bar for each method), you need to have observations in your data corresponding to those pairings. Currently, it looks like each level corresponds to two methods at most. To artificially generate those pairings, you can use tidyr::complete() and tidyr::expand() before plotting. For each new pairing, ave will automatically be assigned NA, but you can change this behavior using the fill parameter in tidyr::complete().
Here's an example where ave is set to 0 for every new pairing instead of NA:
dat %>%
complete(expand(dat, levels, method), fill = list(ave = 0)) %>%
ggplot(df4,mapping = aes(x = as.factor(levels),
y = ave,
fill = as.factor(method),
)) +
geom_bar(stat = "identity", position = position_dodge(width = 1))+
xlab("levels") +
ylab("Mean")

Make boxplots of columns in R

I am a beginner in R, and have a question about making boxplots of columns in R. I just made a dataframe:
SUS <- data.frame(RD = c(4, 3, 4, 1, 2, 2, 4, 2, 4, 1), TK = c(4, 2, 4, 2, 2, 2, 4, 4, 3, 1),
WK = c(3, 2, 4, 1, 3, 3, 4, 2, 4, 2), NW = c(2, 2, 4, 2, NA, NA, 5, 1, 4, 2),
BW = c(3, 2, 4, 1, 4, 1, 4, 1, 5, 1), EK = c(2, 4, 3, 1, 2, 4, 2, 2, 4, 2),
AN = c(3, 2, 4, 2, 3, 3, 3, 2, 4, 2))
rownames(SUS) <- c('Pleasant to use', 'Unnecessary complex', 'Easy to use',
'Need help of a technical person', 'Different functions well integrated','Various function incohorent', 'Imagine that it is easy to learn',
'Difficult to use', 'Confident during use', 'Long duration untill I could work with it')
I tried a number of times, but I did not succeed in making boxplots for all rows. Someone who can help me out here?
You can do it as well using tidyverse
library(tidyverse)
SUS %>%
#create new column and save the row.names in it
mutate(variable = row.names(.)) %>%
#convert your data from wide to long
tidyr::gather("var", "value", 1:7) %>%
#plot it using ggplot2
ggplot(., aes(x = variable, y = value)) +
geom_boxplot()+
theme(axis.text.x = element_text(angle=35,hjust=1))
As #blondeclover says in the comment, boxplot() should work fine for doing a boxplot of each column.
If what you want is a boxplot for each row, then actually your current rows need to be your columns. If you need to do this, you can transpose the data frame before plotting:
SUS.new <- as.data.frame(t(SUS))
boxplot(SUS.new)

how to make barplot bars same size in plot window in R using barplot function

I would like to plot 3 plots in the same window. Each will have a different amount of bar plots. How could I make them all the same size and close together (same distance from each other) without doing NAs in the smaller barplots. example code below. I do want to point out my real data will be plotting numbers from dataframes$columns not a vector of numbers as shown below. I am sure there is magic way to do this but cant seem to find helpful info on the net. thanks
pdf(file="PATH".pdf");
par(mfrow=c(1,3));
par(mar=c(9,6,4,2)+0.1);
barcenter1<- barplot(c(1,2,3,4,5));
mtext("Average Emergent", side=2, line=4);
par(mar=c(9,2,4,2)+0.1);
barcenter2<- barplot(c(1,2,3));
par(mar=c(9,2,4,2)+0.1);
barcenter3<- barplot(c(1,2,3,4,5,6,7));
Or would there be a way instead of using the par(mfrow....) to make a plot window, could we group the barcenter data on a single plot with an empty space between the bars? This way everything is spaced and looks the same?
Using the parameters xlim and width:
par(mfrow = c(1, 3))
par(mar = c(9, 6, 4, 2) + 0.1)
barcenter1 <- barplot(c(1, 2, 3, 4, 5), xlim = c(0, 1), width = 0.1)
mtext("Average Emergent", side = 2, line = 4)
par(mar = c(9, 2, 4, 2) + 0.1)
barcenter2 <- barplot(c(1, 2, 3), xlim = c(0, 1), width = 0.1)
par(mar = c(9, 2, 4, 2) + 0.1)
barcenter1 <- barplot(c(1, 2, 3, 4, 5, 6, 7), xlim = c(0, 1), width = 0.1)
Introducing zeroes:
df <- data.frame(barcenter1 = c(1, 2, 3, 4, 5, 0, 0),
barcenter2 = c(1, 2, 3, 0, 0, 0, 0),
barcenter3 = c(1, 2, 3, 4, 5, 6, 7))
barplot(as.matrix(df), beside = TRUE)
With ggplot2 you can get something like this:
df <- data.frame(x=c(1, 2, 3, 4, 5,1, 2, 3,1, 2, 3, 4, 5, 6, 7),
y=c(rep("bar1",5), rep("bar2",3),rep("bar3",7)))
library(ggplot2)
ggplot(data=df, aes(x = x, y = x)) +
geom_bar(stat = "identity")+
facet_grid(~ y)
For the option you mentioned in your second comment you would need:
x <- c(1, 2, 3, 4, 5, NA, 1, 2, 3, NA, 1, 2, 3, 4, 5, 6, 7)
barplot(x)

Resources