Is there a way to fit this density plot into the frame better? - TagMerge
3Is there a way to fit this density plot into the frame better?Is there a way to fit this density plot into the frame better?

Is there a way to fit this density plot into the frame better?

Asked 1 years ago
0
3 answers

Increase the space between the plot and the axis using scale_y_discrete(expand = expansion(mult = c(0.05, 0.7))). The second element to the mult argument controls the upper limits: set at a high value to illustrate the point - set to suit your aesthetic requirements.

theme_set(theme_bw())

# Filter data and create plot:
work %>% 
  filter(Month_Name %in% c("September",
                           "October",
                           "November",
                           "December",
                           "January",
                           "February")) %>% 
  ggplot(aes(x=Mins_Work,
             y=Month_Name,
             fill = ..x..))+
  geom_density_ridges_gradient(scale = 3,
                               rel_min_height = 0.01,
                               alpha = .5)+
  scale_fill_viridis(option = "B")+
  scale_y_discrete(expand = expansion(mult = c(0.05, 0.7)))+
  labs(title = "Distribution of Recorded Work by Month",
       x="Minutes of Recorded Work",
       y="Month")

Created on 2022-02-24 by the reprex package (v2.0.1)

Source: link

0

A histogram of eruption durations for another data set on Old Faithful eruptions, this one from package MASS:
library(MASS)
histogram(~ duration, data = geyser)
The default setting using geom_histogram are less than ideal:
ggplot(geyser) + geom_histogram(aes(x = duration))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Using a binwidth of 0.5 and customized fill and color settings produces a better result:
ggplot(geyser) +
    geom_histogram(aes(x = duration),
                   binwidth = 0.5, fill = "grey", color = "black")
Reducing the bin width shows an interesting feature:
ggplot(geyser) +
    geom_histogram(aes(x = duration),
                   binwidth = 0.05, fill = "grey", color = "black")
Using the base graphics hist function we can compare the data distribution of parent heights to a normal distribution with mean and standard deviation corresponding to the data:
library(UsingR)
hist(Galton$parent, freq = FALSE)
x <- seq(64, 74, length.out=100)
y <- with(Galton, dnorm(x, mean(parent), sd(parent)))
lines(x, y, col = "red")

Source: link

0

It’s always a good idea to examine our data before we get started plotting. We can read the data into a pandas dataframe and display the first 10 rows:
import pandas as pd# Read in data and examine first 10 rowsflights = pd.read_csv('data/formatted_flights.csv')flights.head(10)
Instead of plotting the bars for each airline side-by-side, we can stack them by passing in the parameter stacked = True to the histogram call:
# Stacked histogram with multiple airlinesplt.hist([x1, x2, x3, x4, x5], bins = int(180/15), stacked=True,         normed=True, color = colors, label=names)
To make density plots in seaborn, we can use either the distplot or kdeplot function. I will continue to use the distplot function because it lets us make multiple distributions with one function call. For example, we can make a density plot showing all arrival delays on top of the corresponding histogram:
# Density Plot and Histogram of all arrival delayssns.distplot(flights['arr_delay'], hist=True, kde=True,              bins=int(180/5), color = 'darkblue',              hist_kws={'edgecolor':'black'},             kde_kws={'linewidth': 4})
Filling in the density plot can help us to distinguish between overlapping distributions. Although this is not always a good approach, it can help to emphasize the difference between distributions. To shade the density plots, we pass in shade = True to the kde_kws argument in the distplot call.
sns.distplot(subset['arr_delay'], hist = False, kde = True,                 kde_kws = {'shade': True, 'linewidth': 3},                   label = airline)

Source: link

Recent Questions on r

    Programming Languages