<- data.frame(
data value1=c(rnorm(50, 8, 5)),
value2=c(rnorm(50, 10, 2))
)
plot(data$value1 ~ data$value2 , # value1: y, value2: x
main = "Streudiagramm", #Überschrift
xlab = "Value2", # x-Achse
ylab= "Value1", # y-Achse
pch=17, # Dreiecke
col= "darkgray" # Farbe
)
abline(lm(data$value1 ~ data$value2, data = data), col="orange", lwd=3)
### Scatterplot mit Boxplot
library(car)
## Loading required package: carData
scatterplot(data$value1 ~ data$value2,
data = data,
main = "Streudiagramm", #Überschrift
xlab = "Value2", # x-Achse
ylab= "Value1", # y-Achse
pch=17, # Dreiecke
col= "darkgray" # Farbe
)
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:car':
##
## logit
scatterHist(data$value1, data$value2,
main = "Streudiagramm", #Überschrift
xlab = "Value1", # x-Achse
ylab= "Value2", # y-Achse
pch=17, # Dreiecke
col= "darkgray" , # Farbe
#ellipse = FALSE, # Keine Kreise
method = "pearson" #-0.11 ->r
)
<- data.frame(
data1 value1=c(rnorm(50, 8, 5)),
value2=c(rnorm(50, 10, 2)),
value3=c(rnorm(50, 18, 5)),
value4=c(rnorm(50, 19, 2)),
sex =c( rep("A",25), rep("B",25))
)
library(car)
scatterplotMatrix(~value1+value2+value3+value4,
regLine=TRUE, smooth=FALSE,
diagonal=list(method="density"), by.groups=TRUE,
data=data1)
<- data.frame(
data name=c( rep("braunhaarig",15),
rep("blond",5),
rep("rothaarig",10),
rep("schwarzhaarig",12),
rep("bunthaarig",4))
)#head(data)
<- table(data$name)
mytable mytable
##
## blond braunhaarig bunthaarig rothaarig schwarzhaarig
## 5 15 4 10 12
<- c("gold", "brown","green","red", "black") #Farbe
myClr
<- paste(names(mytable),#Beschriftung
lbls "\n", #Zeilenumbruch
sep="") #Anzahl der Ausprägungen
mytable,
pie( # Erstelle ein Kreisdiagramm
#Daten
mytable, labels = lbls,#Bezeichnung
col = myClr, #Farbe
main="Kreidiagramm\n Haarfarben")
<- data.frame(
data <- c("blond","braunhaarig","bunthaarig","rothaarig","schwarzhaarig"),
name <- c(mytable[1],mytable[2],mytable[3],mytable[4],mytable[5])
zahl
)
colnames(data)[1] <- "name" #Spaltenname ändern
colnames(data)[2] <- "zahl"#Spaltenname ändern
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(data, aes (x="", y = zahl, fill = factor(name))) + #Daten einlesen
geom_bar(width = 1, stat = "identity") + #immer so
coord_polar("y")+ # Kreisdiagramm
geom_text(aes(label = paste(round(zahl / sum(zahl) * 100, 1), "%")), #Berechnung der Prozente
position = position_stack(vjust = 0.5)) + #Position mittig
theme_classic() + #Farbe
theme(plot.title = element_text(hjust=0.5), #Titel Höhe
axis.line = element_blank(), # keine Linie
axis.text = element_blank())+ # keine Linietext
labs(fill = "Haarfarbe", #Legend
x = NULL, # keine Achsenwerte
y = NULL, # keine Achsenwerte
title = "Kreisdiagramm") # Titel des Kreisdiagramms
<- data.frame(
data name=c( rep("braunhaarig",15),
rep("blond",5),
rep("rothaarig",10),
rep("schwarzhaarig",12),
rep("bunthaarig",4))
)
<-(table(data)/sum(table(data)))*100
mytable <-as.data.frame(mytable)
mytablehead(mytable)
## data Freq
## 1 blond 10.869565
## 2 braunhaarig 32.608696
## 3 bunthaarig 8.695652
## 4 rothaarig 21.739130
## 5 schwarzhaarig 26.086957
library("dplyr")
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
<- mytable %>%
mytable arrange(desc(data)) %>%
mutate(lab.ypos = cumsum(Freq) - 0.5*Freq)
$lab.ypos <-round(mytable$lab.ypos,2)
mytable$Freq <-round(mytable$Freq,2)
mytable mytable
## data Freq lab.ypos
## 1 schwarzhaarig 26.09 13.04
## 2 rothaarig 21.74 36.96
## 3 bunthaarig 8.70 52.17
## 4 braunhaarig 32.61 72.83
## 5 blond 10.87 94.57
ggplot(mytable, aes(x = 2, y = Freq, fill = data)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0)+
geom_text(aes(y = lab.ypos, label = Freq), color = "white")+
scale_fill_brewer(palette="Dark2") +
theme_void()+
xlim(0.5, 2.5) # to create donut pie
<- data.frame(
data23 name=c( rep("braunhaarig",15),
rep("blond",5),
rep("rothaarig",10),
rep("schwarzhaarig",12),
rep("bunthaarig",4))
)#View(data)
<- table(data23$name) counts
barplot(counts , density=c(5,10,20,30,7) , angle=c(0,45,90,11,36) , col="brown" )
library(readxl)
<- read_excel("C:/Users/Alfa/Desktop/Alfa/Musterloesungen/uebung9.xlsx")
uebung9 #View(uebung9)
#View(uebung9)
<- table(uebung9$Geschlecht, by = uebung9$Brille)
counts
barplot(counts, main="Balkendiagramm",
col=c("deepskyblue","tomato"),
ylab="Anzahl Personen",xlab = "Geschlecht und Brille",
beside=T,
names.arg =c("M-Brille","F-Brille","M-ohne-Brille", "F-ohneBrille"))
<- table(uebung9$Geschlecht, by = uebung9$Brille)
counts
barplot(counts, main="Brille und Geschlecht",
col=c("blue","red"),
ylab="Anzahl Personen",xlab = "Bereitschaft eine Brille",
ylim=c(0,130),
beside=FALSE,
names.arg =c("Mit Brille","Ohne Brille"),
legend=TRUE)
<- table( uebung9$Brille, by = uebung9$Geschlecht)
counts#counts
barplot(counts, main="Brille und Geschlecht",
col=c("orange","lightgreen"),
ylab="Anzahl Personen",xlab = "Geschlecht",
ylim=c(0,130),
beside=FALSE,
names.arg =c("Männer","Frauen"),
legend=TRUE)
### Balkendiagramm - auf 1 geeicht
<- table( Brille = uebung9$Brille, Geschlecht = uebung9$Geschlecht)
counts<-(counts)
your_matrix
barplot(prop.table(your_matrix, 2) )
<- c("M-Brille","F-Brille","M-ohne-Brille", "F-ohneBrille")
kurz<- c(70,40,30,60)
Anzahl <- data.frame(kurz,Anzahl) brillegender
barplot(brillegender$Anzahl, names.arg = brillegender$kurz)
<- data.frame(
data name=c( rep("A",50), rep("B",50)),
value=c( rnorm(50, 12, 6), rnorm(50, 19, 10))
)#View(data)
boxplot(data$value ~ data$name, # Daten aufteilen zwischen A und B
main = "Ich bin ein Boxplot", # Überschrift
xlab = "Zufallswerte", # x-Achse
ylab = "Ausprägung", # y-Achse
col = c("lightblue", "blue") # Farbe
)
<- aggregate(value ~ name, data, mean)
means $value<- round(means$value, 2)
meanslibrary(ggplot2)
ggplot(data = data, aes(x=name, #Teilen der Daten
y=value, #Metrische Var.
fill=name)) + #Teilen für die Legende
geom_boxplot()+ #Erstelle ein Boxplot
stat_summary(fun=mean, # Setze Mittelwert
colour="darkred", #Farbe
geom="point", #Punkt
shape=18, # Form des Punktes
size=3) + #Größe
geom_text(data = means, #Setze Text aus der "means"
aes(label = value, #Label
y = value+ 2.00)) #Ort + 2.0 Punkte
ggplot(data, aes(x=name, #Teilen der Daten
y=value, #Metrische Var.
fill=name)) + #Teilen für die Legende
geom_boxplot(
outlier.color="blue",#Ausreißer in Blau
outlier.shape=23, # Form
outlier.size=4)+ # Größe
coord_flip() #Drehen
<- data.frame(
data name=c( rep("A",50), rep("B",50)),
value=c( rnorm(50, 12, 5), rnorm(50, 19, 3))
)
hist(data$value, #Daten
col ="gray", #Farbe
xlab ="Zufallswerte",#x-Achse
ylab ="Anzahl",#y-Achse
main ="Ich bin ein Histogramm",#Überschrift
breaks = 10, #
sub ="Abbildung"
)
<- data$value
x
<-hist(x, breaks=10, col="lightblue", xlab="XX",
hmain="Histogram ",
ylab= "Anzahl")
<-seq(min(x),max(x),length=40)
xfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit<- yfit*diff(h$mids[1:2])*length(x)
yfit lines(xfit, yfit, col="blue", lwd=2)
<- scale(data$value)
x hist(x,
main="Beispiel Histogramm", # Überschrift
xlab="Zufallszahlen", #x-Achse
ylab="Wahrscheinlichkeitsdichte", #y-Achse
col="gray", # Farbe
breaks= seq(-5,5,length=14),
freq=FALSE) #Bereich zur Einteilung
<- mean(x) # Mittelwert von x
m <- sd(x) #Standardabweichung von x
s curve(dnorm(x,m,s),add=TRUE,lwd=3, col="red") # Dichefunktion, Add setzen, Breite: drei, Farbe: Rot
library(dplyr) #Anpassung der Daten. Hier in diesem Beispiel: die Gruppierung
library(ggplot2)
%>% # Datengrundlage
data group_by(name)%>% #Gruppierung
ggplot(aes(value))+ #plot + Daten
geom_histogram( binwidth=3, aes(fill=name), color="#e9ecef", alpha=0.7) + # Erstelle ein Histogramm, Unterteilung, Farbe + Transparenz
facet_wrap(~name)+ # Zwei Graphen
theme_classic()+ #Farbschema
labs(x="Gruppierungen", y="Anzahl") # Beschriftung
library(ggplot2)
ggplot(data, aes(x=value, color=name, fill=name)) +
geom_histogram( binwidth=3, aes(fill=name), color="#e9ecef", alpha=0.7) + # Erstelle ein Histogramm, Unterteilung, Farbe + Transparenz
facet_wrap(~name)+ # Zwei Graphen
theme_classic()+ #Farbschema
labs(x="Gruppierungen", y="Anzahl") + # Beschriftung
scale_color_manual(values=c("#999999", "#E69F00"))+
scale_fill_manual(values=c("#999999", "#E69F00"))
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
<- ddply(data, "name", summarise, grp.mean=mean(value))
mu
ggplot(data, aes(x=value, color=name, fill=name)) +
geom_histogram(binwidth = 4, aes(y=..density..), alpha=0.5,
position="identity")+
geom_density(alpha=.2) +
facet_wrap(~name) # Zwei Graphen
library(ggridges)
library(ggplot2)
ggplot(data, aes(x=value, y =name, fill=name)) +
geom_density_ridges(alpha = 0.6, rel_min_height = 0.01, bandwidth =0.7,
color = "white", from = -5, to = 30) +
theme_ridges(grid = TRUE) + #theme_dark()+
# scale_fill_cyclical() +
labs(x = "XXX", y = "XXX") +theme(legend.position = "right")
%>%
data group_by(name) %>%
ggplot(aes(value, color = name))+
geom_histogram(aes(y = stat(density), color = name),
fill = "white",position = "identity", binwidth = 5)+
geom_density(aes(color = name), size = 1) +
scale_color_manual(values = c("#868686FF", "#EFC000FF","#000099", "#CC0000"))
<-subset(data, data$name =="A")
A <-subset(data, data$name =="B")
B
hist(A$value)
ggplot(data, aes(x=name, y=value, group=1))+
stat_summary(fun.y = mean, geom="point", size=3)+
stat_summary(fun.y = mean, geom="line")+
stat_summary(fun.data = mean_cl_normal, geom="errorbar",width=.2, size=.25)+
labs(x="XX", y="XXX")+
theme_classic()
## Warning: `fun.y` is deprecated. Use `fun` instead.
## Warning: `fun.y` is deprecated. Use `fun` instead.
library(htmltools)
ggplot(data,
aes(x=name,
y=value,
group = name,
color = name,
aesthetics = "fill"
)+
)stat_summary(fun = mean,
geom="point",
size=3
+
)stat_summary(fun = mean,
geom="line",
size= 1.5
+
)stat_summary(fun.data = mean_cl_normal,
geom="errorbar",
width=0.2,
size=1
+
)stat_summary(aes(label=round(..y..,2)),
fun ='mean',
geom="text",
size=4,
position = position_nudge(x = -0.25, y = 0.75)
+
)labs(title = "XXX",
x="XX",
y="XX")+
theme(panel.background = element_rect(size = 0.5, fill = "gray91", colour = "black", linetype='solid'),
panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "darkgray"),
panel.grid.minor = element_line(size = 0.5, linetype = 'solid', colour = "gray"),
)
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
### Plot der Mittelwert - mit Punktwolke
library(ggpubr)
##
## Attaching package: 'ggpubr'
## The following object is masked from 'package:plyr':
##
## mutate
library(cowplot)
##
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggpubr':
##
## get_legend
library(dplyr)
ggline(data, x = "name", y = "value",
add = c("mean_se", "jitter"),
color = "goldenrod",
add.params = list(color = "name", size = 0.9),
palette = "Darjeeling")
### Plot der Mittelwert - mit Extra
library(ggpubr)
library(cowplot)
= ggerrorplot(data, x = "name", y = "value",
p1 desc_stat = "mean_sd",
color = "name", palette = "Set1",
position = position_dodge(0.3)) # Adjust the space between bars
= ggline(data, x = "name", y = "value",
p2 add = c("mean_se", "jitter"),
color = "name", palette = "jco")
plot_grid(p1, p2, labels = "AUTO")
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?