2

I have a dataframe like this.

Heat_map <- c(10, 11, 12, 13, 14)
city1 <-  c(0,1,1,1,0)
city2 <-  c(0,1,0,0,1)
city3 <-  c(0,1,1,0,0)
city4 <-  c(1,1,0,0,0)

city_df <- cbind(Heat_map, city1, city2, city3, city4)
city_df <- as.data.frame(city_df)

I am trying to plot (ggplot or any kind of plot) to show relation between first column, that is heat_map and rest of the columns. I have spend enough time and experiments but I couldn't find a suitable approach for plotting using all columns against one column from same dataframe.

1
  • matplot(city_df[1], city_df[-1], type="l", lty=1, pch=19) maybe? ?matplot generally fits exactly with what you describe wanting to do. Commented Mar 6, 2017 at 4:03

1 Answer 1

3

This is a classic "wide to long" problem: i.e. it's easier to plot if the cities are in one column (named city) and the value for each city in another (value). You can do that using gather from the tidyr package.

Then you can use ggplot2 and either facet_wrap or facet_grid to make a chart for each city.

library(tidyr)
library(ggplot2)

city_df %>% 
  gather(city, value, -Heat_map) %>% 
  ggplot(aes(Heat_map, value)) + geom_point() + facet_wrap(~city)

Result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.