Stapeldiagram

library(ggplot2)
bil <- mtcars # Laddar in datamaterialet
bil$cyl <- as.factor(bil$cyl)
bil$gear <- as.factor(bil$gear)


p <- ggplot(data = bil, aes(x = cyl)) +
  geom_bar(aes(y = stat(count / sum(count))),
           fill = "orange",
           colour = "black")
p

library(ggplot2)
p <- ggplot(data = bil) + aes(x = reorder(cyl,hp),
    y = hp/sum(bil$hp))+ geom_bar(stat = "identity", 
                  fill = "orange",   
                  colour = "black")
p

library(ggplot2)
p <- p + scale_y_continuous(labels = scales::percent)
p

p <- p + theme_bw()
p

p <- p + labs(
  x = "Cylider",
  y = "Procent",
  title = "Andel av hästkrafterna per cylinder",
  caption = "Källa: Egen data"
)
p

p <- p + theme(
  axis.title.y =
    element_text(
      angle = 0,
      hjust = 1,
      vjust = 0.5
    ),
  plot.title = element_text(hjust = 0.5)
)
p

p <- p + theme(
  panel.grid.major.x =
    element_blank(),
  panel.grid.minor.x =
    element_blank(),
  panel.grid.major.y =
    element_line(color = "grey")
)
p

## Roterar diagrammet 90 grader
p + coord_flip()

Grupperat stapeldiagram

library(ggplot2)
p <- ggplot(data = bil)


p <- p + aes(x = gear,                     
            fill = hp,                     
            y=mpg)                         
            

p <- p + geom_bar(stat = "identity", 
                  position = "stack",      
                  col = "black")           

p

library(ggplot2)
library(RColorBrewer)
####  Dodge ####
ggplot(data = mtcars, 
       aes(fill = cyl, y = mpg, x = gear)) +
  geom_bar(position = "dodge", stat = "identity")

data <- mtcars
data$carb <- as.factor(data$carb)

ordnad_data <- data[order(data$gear, 
                          decreasing = TRUE),]


p <- ggplot(data = ordnad_data)


p <- p + aes(x = gear,
             fill = carb,
             y = mpg)


p <-
  p + geom_bar(position = "stack",
               # "stack", "dodge"
               stat = "identity",
               col = "black")
p

p <- p +
scale_fill_manual(name = "Titel",
            values =
  brewer.pal(n = length(unique(data$carb)),
                          name = "Oranges"))
p

p <- p +
  theme_bw() +
  theme(
    axis.title.y =
      element_text(
        angle = 0,
        hjust = 1,
        vjust = 0.5
      ),
    plot.title =
      element_text(hjust = 0.5),
    panel.grid.major.x =
      element_blank()
  ) +
  labs(
    title = 
      "Bilars växlar grupperat på cylindrar",
    y = "", caption = "Källa: Egen data")
p

## Summerar till 100 procent 


data <- mtcars
data$carb <- 
  as.factor(data$carb)

ordnad_data <- 
  data[order(data$gear, decreasing = TRUE),]


p <- ggplot(data = ordnad_data)

# Grupp variabel = fill
p <- p + aes(x = gear,
             fill = carb,
             y = mpg)

p <-
  p + geom_bar(position = "fill",
               stat = "identity",
               col = "black")
p

## Mer instruktioner
p <- p + 
  scale_y_continuous(labels = scales::percent)+
  theme_bw()
p

p <- p +
  labs(
  x = "Växlar",
  y = "Procent",
  title = "Titel här: växlar osv",
  caption = "Källa: Egen data"
)
p

p <- p + theme(
  axis.title.y =
    element_text(
      angle = 0,
      hjust = 1,
      vjust = 0.5
    ),
  plot.title = element_text(hjust = 0.5)
)
p

p <- p + theme(
  panel.grid.major.x =
    element_blank(),
  panel.grid.minor.x =
    element_blank(),
  panel.grid.major.y =
    element_line(color = "grey")
)
p

Lådagram (Box plot)

library(ggplot2)


blom <- iris

## Skapar grunden
p <- ggplot(blom) +
  aes(x = factor(0), y = Sepal.Length) +
  geom_boxplot(fill = "orange")
p

p <- p + scale_x_discrete(breaks = NULL)
p

p <- p + theme_bw() +
  theme(
    axis.title.y =
      element_text(
        angle = 0,
        hjust = 1,
        vjust = 0.5
      ),
    plot.title =
      element_text(hjust = 0.5),
    panel.grid.major.x =
      element_blank()
  ) +
  labs(title = 
    "Fördelning av längden av skälken", x = NULL)
p

g <- ggplot(mpg, aes(class, cty))
g + geom_boxplot(varwidth = T, fill = "plum") +
  labs(
    title = "Box plot",
    subtitle = 
  "City Mileage grouped by Class of vehicle",
    caption = "Source: mpg",
    x = "Class of Vehicle",
    y = "City Mileage"
  )

Violin plot

library(ggplot2)
theme_set(theme_bw())


g <- ggplot(mpg, aes(class, cty))
g + geom_violin() +
  labs(
    title = "Violin plot",
    subtitle = "City Mileage vs Class of vehicle",
    caption = "Source: mpg",
    x = "Class of Vehicle",
    y = "City Mileage"
  )

Histogram

library(ggplot2)

# Laddar in data
blom <- iris
## Skapar grunden
p <- ggplot(blom) +
  aes(Sepal.Length) +
  geom_histogram(fill = "orange",
                 color = "black",
                 bins = 10) 

p

p <- p + scale_y_continuous(expand = c(0, 0),
                            limits = c(0, 40))

p

p <- p + theme_bw() +
  theme(
    axis.title.y =
      element_text(
        angle = 0,
        hjust = 1,
        vjust = 0.5
      ),
    plot.title =
      element_text(hjust = 0.5),
    panel.grid.major.x =
      element_blank(),
    panel.grid.minor.x =
      element_blank(),
    panel.grid.major.y =
      element_line(color = "dark gray")
  ) +
  labs(
    y = "Antal",
    x = "Skälk längd",
    title = "Fördelning av längd på skälk",
    caption = "Källa: Datamaterialet från R"
  )
p

Punktdiagram

library(RColorBrewer)
library(ggplot2)
blom <- iris
## Skapar grunden
p <- ggplot(iris) +
  aes(x = Sepal.Length,
      y = Sepal.Width) +
  geom_point()            

p

## Lägger till ytterligare instruktioner
p <- p + theme_bw() +
  theme(
    axis.title.y =
      element_text(
        angle = 0,
        hjust = 1,
        vjust = 0.5
      ),
    plot.title =
      element_text(hjust = 0.5)
  ) +
  labs(
    title = "Längd på skälken vs bredden",
    y = "Bredd",
    x = "Längd",
    caption = "Källa: Iris"
  )
p

## Skattar en linjär regression utan konfidensband
p_regression <- p + geom_smooth(method = lm,
                                se = FALSE)
p_regression