Reputation: 9
These are my questions:
I really tried everything so far and also looked at almost all videos on stacked bar plot, but without having been able to do it. Something is lacking me.
I will really appreciate your help.
datt <- read.table(text = " Architektur Biologie Mathematik
Baden-Württemberg 4342 7109 7909
Bayern 4377 7295 7014
Berlin 2929 2969 3449
Brandenburg 1148 982 825
Bremen 427 877 738
Hamburg 574 1513 506
Hessen 5155 4426 7617
Mecklenburg-Vorpommern 445 1468 537
Niedersachsen 3102 5353 5464
Nordrhein-Westfalen 9453 14356 18247
Rheinland-Pfalz 1916 2907 3235
Saarland 296 381 334
Sachsen 1182 1354 1521
Sachsen-Anhalt 758 543 349
Schleswig-Holstein 438 1239 1336
Thüringen 1796 921 445
" , header = TRUE)
library(reshape2)
#Zeile
datt$row <- seq_len(nrow(datt))
#
ww<- melt(datt, id.vars = "row")
ww
library(ggplot2)
ggplot(ww , aes(x = variable, y = value, fill = row) ) +
geom_bar( stat = "identity") +
xlab("\nStudienfach WS2019/20") + ylab("Absolute Häufigkeit\n") +
ggtitle("Drei Studiengängen in den verschiedenen Bundesländern WS2019/22")+
theme(plot.title = element_text(hjust=0.5))
+ geom_text(size=3,position = position_stack(vjust = 0.5))
Upvotes: 0
Views: 50
Reputation: 174506
From looking at your data, you should be using row names as categorical variables rather than row numbers, which will be treated as a numeric variable with a gradient fill. This will automatically make the colors more "separated", though you will always have limited color separation with 16 items. It will also label your legend appropriately.
For adding labels to each segment, just add aes(label = value)
to geom_text
.
To change the name of your fill scale to "Bundesländ", use labs(fill = "Bundesländ")
library(reshape2)
library(ggplot2)
ggplot(melt(within(datt, row <- row.names(datt)), id.vars = "row"),
aes(x = variable, y = value, fill = row)) +
geom_col(size = 0.5, col = "gray50") +
geom_text(aes(label = value), size = 3,
position = position_stack(vjust = 0.5)) +
xlab("\nStudienfach WS2019/20") +
ylab("Absolute Häufigkeit\n") +
labs(fill = "Bundesländ") +
ggtitle("Drei Studiengängen in den verschiedenen Bundesländern WS2019/22") +
theme_bw(base_size = 16) +
theme(plot.title = element_text(hjust = 0.5))
Upvotes: 1