简体   繁体   中英

Undesired result displaying ggplot boxplot

I am plotting this data:

Day,Property,Violent
Mon,7.2,5.7
Tue,5,4.5
Wed,6.3,3.6
Thu,5.4,4
Fri,9.5,5.6
Sat,16,10.9
Sun,14.2,8.6

with the following code:

library(ggplot2)
library(reshape)
week <- read.csv("week.csv", header=TRUE)
data.melt <- melt(week,id="Day")

ggplot() +
geom_boxplot(aes(x=Day, y= value, fill= variable), 
             data= data.melt, position = position_dodge(width = .9))
  1. Why do my markers appear on the legend but not on the plot?
  2. How can I logically re-order the days of week beginning from Monday? Any help will be appreciated
DF <- read.table(text="Day,Property,Violent
Mon,7.2,5.7
Tue,5,4.5
Wed,6.3,3.6
Thu,5.4,4
Fri,9.5,5.6
Sat,16,10.9
Sun,14.2,8.6", header=TRUE, sep=",")

#I would consider the weekdays ordered, so let's turn them into an ordered factor.
DF$Day <- ordered(as.character(DF$Day), as.character(DF$Day))

library(ggplot2)
library(reshape2)
data.melt <- melt(DF,id.vars="Day")

ggplot() +
  geom_boxplot(aes(x=Day, y= value, fill= variable), 
               data= data.melt, position = position_dodge(width = .9))

在此处输入图片说明

This works just fine. You don't see much, because you only have one value per box. If you want to actually see the colours, you need more values per day and variable. Alternatively, you could use geom_point :

ggplot() +
  geom_point(aes(x=Day, y= value, colour= variable), 
               data= data.melt, position = position_dodge(width = .9))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM