Assignment

Create a web page presentation using R Markdown that features a plot created with Plotly. Host your webpage on either GitHub Pages, RPubs, or NeoCities. Your webpage must contain the date that you created the document, and it must contain a plot created with Plotly. We would love to see you show off your creativity!

Review criteria

The rubric contains the following two questions:

  1. Does the web page feature a date and is this date less than two months before the date that you’re grading this assignment?
  2. Is the web page a presentation and does it feature an interactive plot that appears to have been created with Plotly?

Summary

I used the swiss data on fertility in switserland in the 1888 (https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/swiss.html). From this data I selected the fertility versus the Agriculture collumn. I colored the data based on the Education. Herefor I translated the education level into low, middle and high. I made the plot in GGplot and plotted through plotly to have an interactive plot.

Loading the data

require(stats)
data(swiss)
head(swiss)
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
##              Infant.Mortality
## Courtelary               22.2
## Delemont                 22.2
## Franches-Mnt             20.2
## Moutier                  20.3
## Neuveville               20.6
## Porrentruy               26.6

Building and plotting with GGplot and Plotly

## translating the education into education levels.
group = cut(swiss$Education, c(0, 11, 20, 100, Inf), right=FALSE)
swiss$Education2 <- as.numeric(group)
swiss$Education[swiss$Education2 == 1] <- "low"
swiss$Education[swiss$Education2 == 2] <- "middle"
swiss$Education[swiss$Education2 == 3] <- "high"
swiss$Education <- as.factor(swiss$Education)

## Ploting the interactif plot
g <- ggplot(swiss, aes(y = Fertility, x = Agriculture, color = Education)) 
g <- g + geom_point() + ggtitle("Swiss Fertility data 1888")
plot_ly(g)

## Building and plotting with only Plotly

Here is the same scatterplot with only plotly. The dots have the name of the state in it as well.

p <- plot_ly(data = swiss, y = Fertility, x = Agriculture, 
             color = Education, 
             type = "scatter", 
             mode = "markers",
             text = rownames(swiss),
             marker = list(size = 10, 
                      line = list(color = 'rgba(152, 0, 0, .8)',
                                   width = 2)))  %>%
        layout(title = 'Swiss Fertility data 1888',
         xaxis = list(title = 'Agriculture',
                      range = c(0, 100)),
         yaxis = list(title = 'Fertility',
                      range = c(30,100)))
p