Thursday, May 26, 2022

Visualize 1D data in R-console on Ubuntu 20.04 ( part 2)

Примеры из http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

options(scipen=999)  # turn-off scientific notation like 1e+48

library(ggplot2)

theme_set(theme_bw())  # pre-set the bw theme.

data("midwest", package = "ggplot2")

# midwest <- read.csv("http://goo.gl/G1K41K")  # bkup data source

# Scatterplot

gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 

  geom_point(aes(col=state, size=popdensity)) + 

  geom_smooth(method="loess", se=F) + 

  xlim(c(0, 0.1)) + 

  ylim(c(0, 500000)) + 

  labs(subtitle="Area Vs Population", 

       y="Population", 

       x="Area", 

       title="Scatterplot", 

       caption = "Source: midwest")

plot(gg)




























library(ggplot2)
data(mpg, package="ggplot2")
# mpg <- read.csv("http://goo.gl/uEeRGu")

mpg_select <- mpg[mpg$manufacturer %in% c("audi", "ford", "honda", "hyundai"), ]

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
g <- ggplot(mpg_select, aes(displ, cty)) + 
  labs(subtitle="mpg: Displacement vs City Mileage",
       title="Bubble chart")

g + geom_jitter(aes(col=manufacturer, size=hwy)) + 
  geom_smooth(aes(col=manufacturer), method="lm", se=F)































library(ggplot2)
library(ggExtra)
data(mpg, package="ggplot2")
# mpg <- read.csv("http://goo.gl/uEeRGu")

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
mpg_select <- mpg[mpg$hwy >= 35 & mpg$cty > 27, ]
g <- ggplot(mpg, aes(cty, hwy)) + 
  geom_count() + 
  geom_smooth(method="lm", se=F)

ggMarginal(g, type = "histogram", fill="transparent")
ggMarginal(g, type = "boxplot", fill="transparent")































library(ggplot2)
library(ggcorrplot)

# Correlation matrix
data(mtcars)
corr <- round(cor(mtcars), 1)

# Plot
ggcorrplot(corr, hc.order = TRUE, 
           type = "lower", 
           lab = TRUE, 
           lab_size = 3, 
           method="circle", 
           colors = c("tomato2", "white", "springgreen3"), 
           title="Correlogram of mtcars", 
           ggtheme=theme_bw)
































Теперь установим

$ sudo apt install libcurl4-openssl-dev

$ sudo apt install libproj-dev

Протестируем

library(ggplot2)

library(plyr)

library(scales)

library(zoo)


df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/yahoo.csv")

df$date <- as.Date(df$date)  # format date

df <- df[df$year >= 2012, ]  # filter reqd years


# Create Month Week

df$yearmonth <- as.yearmon(df$date)

df$yearmonthf <- factor(df$yearmonth)

df <- ddply(df,.(yearmonthf), transform, monthweek=1+week-min(week))  # compute week number of month

df <- df[, c("year", "yearmonthf", "monthf", "week", "monthweek", "weekdayf", "VIX.Close")]

head(df)

#>   year yearmonthf monthf week monthweek weekdayf VIX.Close

#> 1 2012   Jan 2012    Jan    1         1      Tue     22.97

#> 2 2012   Jan 2012    Jan    1         1      Wed     22.22

#> 3 2012   Jan 2012    Jan    1         1      Thu     21.48

#> 4 2012   Jan 2012    Jan    1         1      Fri     20.63

#> 5 2012   Jan 2012    Jan    2         2      Mon     21.07

#> 6 2012   Jan 2012    Jan    2         2      Tue     20.69

# Plot

ggplot(df, aes(monthweek, weekdayf, fill = VIX.Close)) + 

  geom_tile(colour = "white") + 

  facet_grid(year~monthf) + 

  scale_fill_gradient(low="red", high="green") +

  labs(x="Week of Month",

       y="",

       title = "Time-Series Calendar Heatmap", 

       subtitle="Yahoo Closing Price", 

       fill="Close")































===================================
Теперь можем выполнить и билд tidyverse
===================================
install.packages("tidyverse") via R-console

Тест кода

library(tidyverse)
surveys_complete <- read_csv("./surveys_complete.csv")
yearly_sex_counts <- surveys_complete %>%
                      count(year, genus, sex)
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(facets =  vars(genus))     










Run via CLI 
























No comments:

Post a Comment