Reputation: 33
would like to: add a legend for the fill, move my axis labels and title, use specific colors for the three fill values
Code below
#adding data and libraries
library(tidyverse)
library(plotly)
library(here)
library(dplyr)
trails = read.csv("../input/national-park-trails/AllTrails data -nationalpark.csv")
summary(trails)
head(trails)
#subsets
ca_trails = trails %>% filter(state_name=="California") #filter data set to CA
JT_trails = ca_trails %>% filter(area_name=="Joshua Tree National
Park") #filter set to JT
head(JT_trails)
#ggplot
JT_trailgraph <- ggplot(JT_trails, aes(x=difficulty_rating, y=name)) +
geom_tile(aes(fill = visitor_usage)) +
labs(title = "Joshua Tree Trails", subtitle = "Difficulty Rating Compared to Usage",
y = "Name", x = "Difficulty Rating")
ggplotly(JT_trailgraph, width = 700, height = 1500)
dput(head(JT_trails)
structure(list(trail_id = c(10011170L, 10031888L, 10035554L,
10003872L, 10019827L, 10034036L), name = c("Ryan Mountain Trail",
"Barker Dam Nature Trail", "Hidden Valley Nature Trail", "Lost Palms Oasis",
"Arch Rock Nature Trail", "Fortynine Palms Oasis Trail"), area_name = c("Joshua Tree National Park",
"Joshua Tree National Park", "Joshua Tree National Park", "Joshua Tree National Park",
"Joshua Tree National Park", "Joshua Tree National Park"), city_name = c("Twentynine Palms",
"Twentynine Palms", "Twentynine Palms", "Indio", "Twentynine Palms",
"Twentynine Palms"), state_name = c("California", "California",
"California", "California", "California", "California"), country_name = c("United States",
"United States", "United States", "United States", "United States",
"United States"), X_geoloc = c("{'lat': 34.00264, 'lng': -116.13594}",
"{'lat': 34.02511, 'lng': -116.14173}", "{'lat': 34.01234, 'lng': -116.16807}",
"{'lat': 33.73685, 'lng': -115.81058}", "{'lat': 33.984140000000004, 'lng': -116.01655}",
"{'lat': 34.11922, 'lng': -116.11198}"), popularity = c(37.0218,
30.1796, 27.9587, 26.1245, 24.81, 24.2376), length = c(4828.02,
2896.812, 1609.34, 11587.248, 1126.538, 4506.152), elevation_gain = c(325.8312,
19.812, 34.7472, 312.7248, 7.9248, 188.976), difficulty_rating = c(3L,
1L, 1L, 3L, 1L, 3L), route_type = c("out and back", "out and back",
"loop", "out and back", "out and back", "out and back"), visitor_usage = c(3L,
3L, 3L, 2L, 2L, 3L), avg_rating = c(4.5, 4.5, 4.5, 4.5, 4.5,
4.5), num_reviews = c(917L, 480L, 500L, 295L, 251L, 381L), features = c("['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'forest', 'lake', 'kids', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']", "['dogs-no', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']", "['dogs-no', 'kids', 'views', 'wildlife']"
), activities = c("['hiking']", "['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running', 'walking']",
"['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running', 'walking']",
"['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running']",
"['birding', 'camping', 'hiking', 'nature-trips', 'walking']",
"['hiking', 'nature-trips', 'trail-running']"), units = c("i",
"i", "i", "i", "i", "i")), row.names = c(NA, 6L), class = "data.frame")
finally got to editing but stack wants me to write more words..... so I am adding more words to my post -_- even more words are needed. I shall add more.
Upvotes: 0
Views: 80
Reputation: 12739
The question does not state the expected position of the titles or axis labels. So I've shown them moved: adjust to suit your preferred position.
Visitor usage is assumed to be one of three values: 1:3; I've manually added a 1L to the data so that the graph displays all three cases.
Again adjust colours and labelling to suit your own requirements.
library(ggplot2)
ggplot(JT_trails, aes(x=difficulty_rating, y=name)) +
geom_tile(aes(fill = factor(visitor_usage))) +
scale_fill_manual(breaks = factor(1:3),
values = c("red", "blue", "green"),
labels = c("High", "Moderate", "Low"))+
labs(title = "Joshua Tree Trails",
subtitle = "Difficulty Rating Compared to Usage",
y = "Name",
x = "Difficulty Rating",
fill = "Visitor usage") +
theme_bw()+
theme(legend.position = "bottom",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 1),
axis.title.x = element_text(hjust = 1),
axis.title.y = element_text(hjust = 0))
data
JT_trails <- structure(list(trail_id = c(10011170L, 10031888L, 10035554L,
10003872L, 10019827L, 10034036L),
name = c("Ryan Mountain Trail", "Barker Dam Nature Trail", "Hidden Valley Nature Trail", "Lost Palms Oasis",
"Arch Rock Nature Trail", "Fortynine Palms Oasis Trail"),
area_name = c("Joshua Tree National Park",
"Joshua Tree National Park", "Joshua Tree National Park", "Joshua Tree National Park",
"Joshua Tree National Park", "Joshua Tree National Park"), city_name = c("Twentynine Palms",
"Twentynine Palms", "Twentynine Palms", "Indio", "Twentynine Palms",
"Twentynine Palms"), state_name = c("California", "California",
"California", "California", "California", "California"), country_name = c("United States",
"United States", "United States", "United States", "United States",
"United States"), X_geoloc = c("{'lat': 34.00264, 'lng': -116.13594}",
"{'lat': 34.02511, 'lng': -116.14173}", "{'lat': 34.01234, 'lng': -116.16807}",
"{'lat': 33.73685, 'lng': -115.81058}", "{'lat': 33.984140000000004, 'lng': -116.01655}",
"{'lat': 34.11922, 'lng': -116.11198}"), popularity = c(37.0218,
30.1796, 27.9587, 26.1245, 24.81, 24.2376), length = c(4828.02,
2896.812, 1609.34, 11587.248, 1126.538, 4506.152), elevation_gain = c(325.8312,
19.812, 34.7472, 312.7248, 7.9248, 188.976), difficulty_rating = c(3L,
1L, 1L, 3L, 1L, 3L), route_type = c("out and back", "out and back",
"loop", "out and back", "out and back", "out and back"), visitor_usage = c(3L,
3L, 3L, 2L, 2L, 1L), avg_rating = c(4.5, 4.5, 4.5, 4.5, 4.5,
4.5), num_reviews = c(917L, 480L, 500L, 295L, 251L, 381L), features = c("['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'forest', 'lake', 'kids', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']", "['dogs-no', 'views', 'wild-flowers', 'wildlife']",
"['dogs-no', 'kids', 'views', 'wild-flowers', 'wildlife']", "['dogs-no', 'kids', 'views', 'wildlife']"
), activities = c("['hiking']", "['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running', 'walking']",
"['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running', 'walking']",
"['birding', 'hiking', 'nature-trips', 'rock-climbing', 'trail-running']",
"['birding', 'camping', 'hiking', 'nature-trips', 'walking']",
"['hiking', 'nature-trips', 'trail-running']"), units = c("i",
"i", "i", "i", "i", "i")), row.names = c(NA, 6L), class = "data.frame")
Created on 2022-10-13 with reprex v2.0.2
Upvotes: 0