将aes(x = ...)放在ggplot()或geom()中的区别

时间:2018-05-07 01:44:29

标签: r ggplot2 histogram

在ggplot()或geom()中放置aes(x = ...)有什么区别(例如下面的geom_histogram()):

1。在ggplot()中:

ggplot(diamonds) + 
  geom_histogram(binwidth=500, aes(x=diamonds$price))+ 
  xlab("Diamond Price U$") + ylab("Frequency")+ 
  ggtitle("Diamond Price Distribution")

The histogram of method A goes here

2。在geom()中:

ggplot(diamonds, aes(x=diamonds$price)) + 
  geom_histogram(bidwidth= 500) + 
  xlab("Price") + ylab("Frequncy") + 
  ggtitle("Diamonds Price distribution")

The histogram of method B goes here

1 个答案:

答案 0 :(得分:2)

如果您有多个具有不同映射的geom,那么您是将jQuery -> $('#plan_material_tokens').tokenInput('/materials.json', { propertyToSearch: 'title' }) zindex: 6000 allowCustomEntry: false preventDuplicates: true prePopulate: $("#plan_material_tokens").data('pre') 置于原始x = price调用还是特定ggplot()中才真正重要。您在geom调用中指定的映射将应用于所有geom,因此通常最好将映射放在顶层,如果只是为了节省您必须为每个单独的geom再次键入它。如果仅适用于特定ggplot(),请在单个geom中指定映射。

另请注意,它应该只是geom,而不是aes(x = price)aes(x = diamonds$price)知道查看您正在使用的数据框作为ggplot参数。如果你像data那样手动传递矢量,你可能会在更复杂的情节中弄乱分面或分组。