简体   繁体   English

将es(x = ...)放入ggplot()或geom()之间的区别

[英]Difference between putting aes(x=…) in ggplot() or in geom()

What is the difference between putting aes(x=…) in ggplot() or in geom() (eg geom_histogram() below): 将aes(x =…)放入ggplot()或geom()(例如下面的geom_histogram())之间有什么区别:

1. in ggplot(): 1.在ggplot()中:

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

方法A的直方图在这里

2. in the geom(): 2.在geom()中:

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

方法B的直方图在这里

Whether you put x = price in the original ggplot() call or in a specific geom only really matters if you have multiple geoms with different mappings. 无论你把x = price在原来的ggplot()调用或在特定geom如果你有不同的映射多个geoms唯一真正重要的。 The mapping you specify in the ggplot() call will be applied to all geoms, so it's often best to put the mapping in the top level like that, if only to save you having to type it out again for each individual geom. 您在ggplot()调用中指定的映射将应用于所有geom,因此通常最好将映射放在这样的顶层,如果这样做只是为了节省您必须为每个单独的geo再次键入它。 Specify mappings in the individual geom s if they only apply to that specific geom . 如果仅适用于特定geom映射,则在各个geom指定映射。

Also note that it should just be aes(x = price) , not aes(x = diamonds$price) . 还要注意,它应该只是aes(x = price) ,而不是aes(x = diamonds$price) ggplot knows to look in the dataframe you're using as your data argument. ggplot知道要查看用作data参数的数据ggplot If you pass a vector manually like diamonds$price you might mess up facetting or grouping in a more complex plot. 如果您手动传递矢量(例如diamonds diamonds$price ,则可能会使构面混乱或在更复杂的情节中分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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