Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用directlabels+ggplot时的数据框形状_R_Ggplot2 - Fatal编程技术网

使用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)