问题描述
使用此可复制示例tibble
# install and attach packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,dplyr,ggplot2)
# Create example dataset (tib1)
tib1 = tibble(location = c("loc1",rep(c("loc1","loc2"),8),rep("loc2",2)),drill = sort(c(1,rep(1:4,4),4,4)),thickness = c(20,34,99,67,29,22,53,93,64,98,76,42,49,23,11,74,19,50,40),soiltype = c("gravel",rep( c("sand","loam","clay"),5 ),"sand","gravel","clay")) %>%
arrange(location,drill)
tib1 <- tib1 %>% group_by(location,drill) %>%
mutate(order = row_number(),bottom_of_layer = cumsum(thickness),top_of_layer = bottom_of_layer-thickness) %>%
ungroup %>%
select(location,drill,bottom_of_layer,top_of_layer,thickness,soiltype,order)
tib1
我想绘出土壤的横截面。我尝试使用geom_bar
中的geom_col
或ggplot2
来做到这一点。在示例数据中,给出的顺序为(1:3)。我希望这些条以“订单列”中指定的顺序堆叠。
因此,这意味着该酒吧在哪里:
- location ==“ loc2” 和 drill == 4 的着色顺序应为(从上到下):
橙色(壤土)-黄色(沙子)-灰色(砾石)-海水绿色(粘土)
AND
- location ==“ loc1” 和 drill == 1 的着色顺序应为(从上到下):
灰色(砾石)-黄色(沙子)-海水绿色(粘土)
其他堆叠的条形保持相同的顺序和颜色。
换句话说:我需要堆叠栏中的颜色与“订单”列中指定的顺序一起变化。
colpalette = c("darkseagreen3","darkgrey","#FF9966","palegoldenrod")
ggplot(tib1,aes(x = drill))+
geom_bar(aes(fill = soiltype,y = -thickness),stat = "identity")+
scale_fill_manual(values = colpalette)+
facet_wrap(vars(location),scale = "free_x")+
xlab("drill")+
ylab("depth (cm)")+
ggtitle("how to plot the bars in the 'preferred order'? ",subtitle = "the order of loc2 and drill == 4 should be: loam-sand-gravel-clay")+
theme_minimal()
可比,但略有不同的问题是:
Order of stacked bars ggplot2 - Soil profile 和 Change the order of Stacked Bar Chart in ggplot2
我想知道我所要的东西是否完全可以在ggplot中实现为:
在多个条形图放置在上方时,条形图会自动堆叠 相同的位置。填充顺序旨在匹配 传说。
因此,我可能不得不寻找其他选择...除非有人遭到黑客入侵? 任何帮助,答案,替代绘图选项,链接表示赞赏:) 如果可能的话,我更喜欢ggplot2。
解决方法
您应该订购级别:
即:
create or replace procedure add_services (
p_res_id in reservation.res_id%type,p_room_type in room.room_type%type,p_room_num in room.room_num%type
)
is
begin
insert into reservation(res_id,room_type,room_date,room_num)
values(p_res_id,p_room_type,sysdate,p_room_num);
commit;
end;
,
当您希望填充或颜色随 fill 和或 geom_col
和geom_bar
是不合适的ggplot2
函数em> color 分组在一个栏中。 geom_rect
能够在条形/矩形内分隔两层 fill 或 color 。
p1 <- ggplot(tib1)+
geom_rect(data = tib1,aes(x = NULL,NULL,xmin = drill-0.45,xmax = drill+0.45,fill = soiltype,ymin = -bottom_of_layer,ymax = -top_of_layer))+
scale_fill_manual(values = colpalette)+
facet_wrap(vars(location))+
xlab("drill")+
ylab("depth (cm)")+
ggtitle("how to plot the bars in the 'preferred order'? ",subtitle = "the order of loc2 and drill == 4 should be: loam-sand-gravel-clay")+
theme_minimal()
# Optional:
# Adding in text the column called "order"
p1 + geom_text(aes(x = drill,y = (bottom_of_layer + top_of_layer)/-2,label = order))
这对于离散的x轴值也是可能的: how do I use geom_rect with discrete axis values