R 如何将因子转换为ggplot中的数字?

R 如何将因子转换为ggplot中的数字?,r,ggplot2,R,Ggplot2,我有一个数据集(称为nordland),它有两列;公司名称和日期。 我想把它绘制成ggplot,这样它就可以显示在给定日期内有多少家公司。我该怎么做 当我运行此命令时: plot <- ggplot(nordland, aes(x = nordland$Date, y = nordland$`Firm name`)) + geom_col() + labs(x = "Date", y = "Number of firms", Title = "Number of new firms per

我有一个数据集(称为nordland),它有两列;公司名称和日期。 我想把它绘制成ggplot,这样它就可以显示在给定日期内有多少家公司。我该怎么做

当我运行此命令时:

plot <- ggplot(nordland, aes(x = nordland$Date, y = nordland$`Firm name`)) +
geom_col() +
labs(x = "Date", y = "Number of firms", Title = "Number of new firms per month")

plot考虑
geom\u bar
(其中
geom\u col
是它的包装,其中
stat=“identity”
需要从数据集映射y和x)。但是,您只需要带计数的x和带默认值的
stat=“count”
?几何图形条

此外,切勿在
aes()
中使用
$
引用,而是使用不带引号的数据框列名,并避免将对象命名为内置函数,如base R的
plot()
,这可能会导致命名冲突。最后,参数标题应为小写,R区分大小写

myplot <- ggplot(nordland, aes(x = Date)) +
           geom_bar() +
           labs(x = "Date", y = "Number of firms", 
                title = "Number of new firms per month") +
           theme(plot.title = element_text(hjust = 0.5))

考虑
geom\u bar
(其中
geom\u col
是它的包装,其中
stat=“identity”
要求从数据集映射y和x)。但是,您只需要带计数的x和带默认值的
stat=“count”
?几何图形条

此外,切勿在
aes()
中使用
$
引用,而是使用不带引号的数据框列名,并避免将对象命名为内置函数,如base R的
plot()
,这可能会导致命名冲突。最后,参数标题应为小写,R区分大小写

myplot <- ggplot(nordland, aes(x = Date)) +
           geom_bar() +
           labs(x = "Date", y = "Number of firms", 
                title = "Number of new firms per month") +
           theme(plot.title = element_text(hjust = 0.5))

总数据从哪里来?如果你能在你的问题中分享这一点,比如
dput(head(totaldata))
我忘了把它加回去了,对不起!它现在应该是可见的。当然,它是现在添加的。
totaldata
来自哪里?如果你能在你的问题中分享这一点,比如
dput(head(totaldata))
我忘了把它加回去了,对不起!它现在应该是可见的。当然,它现在被添加了。啊,那么简单!非常感谢!:)伟大的很乐意帮忙。请参阅编辑有关标题的内容,该标题应为小写,以便在图形上呈现。啊,所以很简单!非常感谢!:)伟大的很乐意帮忙。请参见编辑有关标题的信息,该标题应为小写,以便在图形上呈现。
nordland$N <- 1
myplot <- ggplot(nordland, aes(x = Date, y = N)) +
          geom_col() +
          labs(x = "Date", y = "Number of firms",
               title = "Number of new firms per month")
           theme(plot.title = element_text(hjust = 0.5))