使用directlabels+ggplot时的数据框形状
我有以下数据帧使用directlabels+ggplot时的数据框形状,r,ggplot2,R,Ggplot2,我有以下数据帧 # Data Year <- c(1500,1750,2000) Country1 <- c(10,20,30) Country2 <- c(30,50,40) df <- data.frame(Year, Country1, Country2) 哪个扔 Error in direct.label.ggplot(myplot) : Need colour aesthetic to infer default direct labels. 我想问题
# Data
Year <- c(1500,1750,2000)
Country1 <- c(10,20,30)
Country2 <- c(30,50,40)
df <- data.frame(Year, Country1, Country2)
哪个扔
Error in direct.label.ggplot(myplot) :
Need colour aesthetic to infer default direct labels.
我想问题是,我的数据框对于ggplot来说形状不好,因为我的标签是列名
我应该重塑我的数据框架吗?如何进行?我认为重塑的最简单方法是使用重塑2软件包 通过这种方式,我们可以提供direct.labels函数所需要的适当的颜色美感
# Add labels
require(directlabels)
direct.label(myplot)
Error in direct.label.ggplot(myplot) :
Need colour aesthetic to infer default direct labels.
df<-data.frame(
Year = c(1500, 1750, 2000),
Country1 = c(10, 20, 30),
Country2 = c(30, 50, 40)
)
#reshape
library(reshape2)
mm<-melt(df, id.var="Year")
#plot
myplot2 <- ggplot(mm, aes(Year, value, color=variable))+ geom_line() + labs(y = "Cows")
#add labels
library(directlabels)
direct.label(myplot2)