一般的な原則は、あなたがその列を持っていたら、あなたが好きなアプローチ使用して描画することができjust like @PoGibas did.、あなたのデータフレームにカラーリング情報を保持する列を追加する必要があるなど、選択した方法がgeom_col()
になります。
brand=c('MS', 'Google', 'Apple', 'MS', 'FB', 'Apple', 'Oracle')
product=c('OS', 'Search', 'Iphone', 'Search', 'Network', 'OS', 'DB')
df= data.frame(brand, product)
# Define color
df$myColor <- NA
foo <- df$product %in% names(which(table(df$product) > 1))
df$myColor[foo] <- df$product[foo]
df
# brand product myColor
#1 MS OS 4
#2 Google Search 5
#3 Apple Iphone NA
#4 MS Search 5
#5 FB Network NA
#6 Apple OS 4
#7 Oracle DB NA
以下は、オリジナルのプロットコードです。わずかに変更されています。ベンダーごとに各製品を1回しか持っていない場合は、カウントするコードを追加するのではなく、aes()
ステートメントにy = 1
を設定することをお勧めします。
library(dplyr)
library(ggplot2)
ggplot(df, aes(x = product, y = 1, fill = factor(myColor))) +
geom_col() +
facet_wrap(~brand, ncol = 5, scales = "free_x") +
guides(fill = "none") +
coord_flip()
![enter image description here](https://i.stack.imgur.com/GSCp6.png)
そして今、我々は物事が良く見えるようにテーマを少しいじっすることができます。
ggplot(df, aes(x = brand, y = product, fill = factor(myColor))) +
geom_tile(width = 0.9, height = 0.9) +
guides(fill = "none") +
scale_x_discrete(name = "", position = "top") +
scale_y_discrete(name = "") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(color = "black"),
panel.background = element_rect(fill="gray95"))
:あなたが使用したりgeom_point()
@PoGibasにより示唆されるように、しかしgeom_tile()
しかし
ggplot(df, aes(x = product, y = 1, fill = factor(myColor))) +
geom_col() +
facet_wrap(~brand, ncol = 5) +
guides(fill = "none") +
scale_y_continuous(breaks = NULL, name = "") +
scale_x_discrete(expand = c(0, 0)) +
coord_flip() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(color = "black"),
strip.background = element_blank(),
panel.background = element_rect(fill="gray95", color="gray95"),
panel.spacing = unit(0, "pt"))
![enter image description here](https://i.stack.imgur.com/qePFH.png)
、このアプリケーションのための正しいGEOMは、私の心の中で、どちらもgeom_col()
です
geom_tile()
を使用すると、塗りつぶし領域の高さと幅、プロットのサイズを変更したりアスペクト比を変更すると、奇妙な驚きはありません。
説明、素晴らしい答えをありがとう... – Vineet