library(ggplot2)
library(tidyr)
library(ggpubr)
data<-read.csv('coffee.csv')

Plot 1

# boxplot

p1<-ggplot(data=data)+
  geom_boxplot(aes(x=avg_rating,y=hhincome,group=avg_rating),color='black',fill='white',size=0.3)+
  theme(axis.title = element_text(size=8),axis.text = element_text(size=5))
p1

From the plot, When the coffee shop has a rating of 4, the household income is the highest. When the coffee shop is rated 1, the income is the lowest. The highest values of income are found in where coffee shops are rated 4 and the lowest values are found in where coffee shops are rated 1. When scoring 1-4, the income rises with the score. However, when reaching a score of 5, income fell.

Plot 2

p2<-ggplot(data=data,aes(x=avg_rating,y=hhincome,group=avg_rating))+
  geom_boxplot(color='black',fill='white')+
  scale_y_continuous(limits = c(0, 250000), breaks = c(50000, 100000, 150000, 200000))+
  facet_wrap(~county)+
  labs(y='Median Annual Household Income($)',x='Average Yelp Rating', size=0.5)
p2

From the plot, it can be seen that the distribution of income in Clayton county is most concentrated, and the distribution of income in Fulton county has a larger range and is on the high side among the five counties. among the five counties, the pattern of change of income with the rating is most obvious in Gwinnett, with the highest value and the mean being highest when the rating is 3. Cobb, DeKalb and Fulton all have the highest values of income at a rating of 4. Fulton county has an insignificant relationship between income and rating, with income basically staying at one level, and a slightly higher income at a rating of 2.

Plot 3

p3<-ggplot(data=data,group=review_count_log)+
  geom_point(mapping = aes(x=review_count_log,y=hhincome,color=pct_white),size=3,alpha = 0.7)+
  facet_wrap(~county)+
  labs(x = "Review Count(log)",
       y = "Median Annual Household Income",
       color = "Proportion of residents\n who self-identified as white",
       title = "Scatterplot: Review Count vs. Household Income") +
  scale_color_gradient(low='blue',high = 'red')+
  theme_bw()+
  theme(axis.text = element_text(size = 6),
        axis.title = element_text(size=8),
        plot.title = element_text(size=10),
        legend.title = element_text(size=8),
        legend.text = element_text(size=6),
        strip.text = element_text(size = 8),
        legend.key.size = unit(0.5,"cm"))
  

options(
  repr.plot.width = 20,
  repr.plot.height = 3)
p3

ggsave('p3.jpg',p3,width = 10,height = 5)

From the graph, overall, low income mainly corresponds to places with low percentage of whites. cobb, gwinnett have relatively high percentage of whites, and the income is located in 5000-20000, which is more concentrated. fulton, dekalb, clayton have lower percentage of whites, and fulton has the highest INCOME. When review count log is 2-4, incomes are higher.

Plot 4

long_data<-pivot_longer(data=data,cols=c(hhincome,pct_pov_log,pct_white,race.tot),names_to='variable',values_to='value')


p4<-ggplot(data =long_data, mapping = aes(x=review_count_log, y=value,color=county)) +
  geom_point(size=1) + 
  geom_smooth(mapping = aes(color = county), method = "lm",se = FALSE,size=0.8) +
  labs(x = "Review Count Logged",
       y = "Values",
       color = "County",
       title = "Sactterplot between logged review count&neighborhood characteristics",
       subtitle = "Using Yelp data in Five Counties Around Atlanta, GA"
       )+
  facet_wrap(~variable, ncol = 2, nrow = 2, scales = "free_y",labeller =labeller(variable = c("hhincome" = "Median Annual Household Income($)", "pct_pov_log" = "Percent Residents Under Poverty", "pct_white" = "Percent White Resident", "race.tot" = "Total Population")))+
  theme(legend.text = element_text(size=6),
        legend.key.size = unit(0.5,"cm"))+
  theme_bw()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# add r and p value on the plot
p4_plus<-p4+stat_cor(label.x.npc = 0.05, label.y.npc = 0.95, method = "pearson",color='black',size=3)

p4_plus
## `geom_smooth()` using formula = 'y ~ x'

ggsave('p4.jpg',p4_plus,width = 10,height = 7)
## `geom_smooth()` using formula = 'y ~ x'

By regression analysis, the correlation between median annual hosehold income and review count logged is weak, with scattered distribution dispersed. Only Dekalb’s correlation will be slightly stronger and positive.

The percent residents under poverty is negatively correlated with the number of reviews logged. With the higher the number of reviews logged, the higher the percent residents under poverty. And Dekalb has the highest degree of negative correlation among all 5 counties.

The percent of white resident is positively correlated with the number of reviews logged. Dekalb county shows the strongest correlation, followed by Fulton county.

The total population is not significantly correlated with the number of comments.The distribution of point is more concentrated than the other three variables.