Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

r - Creating a facet theme/design plot in ggplot2 without using facet_

Is there any possibility to create a facet_wrap looking plot in ggplot2 without using facet_wrap() The reason I would like to achieve this is to match some other design. In the plot without_facet below, can I somehow add "Setosa" in the top, so it looks like the with_facet plot, without using facet_wrap.

library(ggplot2)

df <- iris[iris$Species == 'setosa', ]

with_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +facet_wrap(~Species)
with_facet

without_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try

ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
   geom_point() + 
   ggtitle("setosa")  + 
   theme(plot.title = element_text(hjust = 0.5))

enter image description here

A more "hackish"-one could be this hardcoded approach:

ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point() + 
  ggtitle("setosa")  + 
  geom_rect(xmin = 4.225, xmax = 5.875 , ymin=4.5, ymax=4.6, fill ="lightgrey") + 
  coord_cartesian(clip = 'off', expand = 0.05) +
  theme(plot.title = element_text(hjust = 0.5, size = 12),
        plot.margin = margin(t = 30, r = 20, b = 20, l = 20, unit = "pt"))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...