7

私は (実際には非常に一般的な) 視覚化を行う方法を探しています。これにより、N 個のユニットの人口をいくつかのカテゴリに分割し、N 個のピクトグラムのセットを介して表示されます。新聞などでは、n 番目のユニットのカテゴリに応じて各ピクトグラムが色付けされた、小さな人型の形状が表示される場合があります。N を 1 または 100 に固定し、カウントではなく分数またはパーセンテージを表示するチャートで問題ありません。色付きの「ストライプ」は、複数の行に折り返す必要があります。Rパッケージで利用可能なこのチャートを知っている人はいますか?

具体例を挙げると、

x = サンプル (c("A","B"),100,replace = T)

10x10 (または 5x20 など) の色付きの正方形のグリッドを表示したいと思います。一部は "A" に対応し、緑色、他は赤色です。緑 (および同様に赤) の四角形は、「束ねる」か、元の順序のままにすることができます。

ありがとうございました。

4

4 に答える 4

8

以下に 3 つのバージョンを示します。最初は四角形、次は丸、次は男性のアイコン、最後はスマイリー フェイスを使用します。

正方形

# input data
set.seed(123)
x <- sample(c("A","B"),100,replace = T)

# input parameters - nr * nc should equal length(x)
cols <- c("green", "red")
nr <- 10
nc <- 10

# create data.frame of positions and colors
m <- matrix(cols[factor(x)], nr, nc)
DF <- data.frame(row = c(row(m)), col = c(col(m)[, nc:1]), value = c(m), 
      stringsAsFactors = FALSE)

# plot squares - modify cex to get different sized squares
plot(col ~ row, DF, col = DF$value, pch = 15, cex = 4, asp = 1,
     xlim = c(0, nr), ylim = c(0, nc),
     axes = FALSE, xlab = "", ylab = "")

ここに画像の説明を入力

サークル

# plot circles
plot(col ~ row, DF, col = DF$value, pch = 20, cex = 6, asp = 1,
     xlim = c(0, nr), ylim = c(0, nc),
     axes = FALSE, xlab = "", ylab = "")

ここに画像の説明を入力

png アイコンこのソリューションでは、現在のディレクトリに として保存されていると仮定した男性の白黒アイコンを使用しますman.png。赤と緑に色付けし、正方形または円の代わりにこれら 2 つのバージョンを使用します。

# blank graph to insert man icons into
plot(col ~ row, DF, col = DF$value, asp = 1,
     xlim = c(0, nr), ylim = c(0, nc),
     axes = FALSE, xlab = "", ylab = "", type = "n")

library(png)
man <- readPNG("man.png")

red.man <- man
red.man[,,1] <- man[,,4] # fill in red dimension
R <- subset(DF, value == "red")
with(R, rasterImage(red.man, 
     row-.5, col-.5, row+.5, col+.5, 
     xlim = c(0, nr), ylim = c(0, nc),
     xlab = "", ylab = ""))

green.man <- man
green.man[,,2] <- man[,,4] # fill in green dimension
G <- subset(DF, value == "green")
with(G, rasterImage(green.man, 
     row-.5, col-.5, row+.5, col+.5, 
     xlim = c(0, nr), ylim = c(0, nc),
     xlab = "", ylab = ""))

ここに画像の説明を入力

スマイリー アイコンこのソリューションでは、現在のディレクトリにおよびとして保存されていると仮定した、緑色のスマイリー アイコン赤いしかめっ面アイコンを使用します。smiley_green.jpgsmiley_red.jpg

# blank graph to insert man icons into
xp <- 1.25
plot(col ~ row, DF, col = DF$value, asp = 1,
     xlim = c(0, xp * nr), ylim = c(0, xp * nc),
     axes = FALSE, xlab = "", ylab = "", type = "n")

library(jpeg)
smiley_green <- readJPEG("smiley_green.jpg")
smiley_red <- readJPEG("smiley_red.jpg")    

R <- subset(transform(DF, row = xp * row, col = xp * col), value == "red")
with(R, rasterImage(smiley_red,
     row - .5, col - .5, row + .5, col + .5, 
     xlim = c(0, xp * nr), ylim = c(0, xp * nc),
     xlab = "", ylab = ""))

G <- subset(transform(DF, row = xp * row, col = xp * col), value == "green")
with(G, rasterImage(smiley_green,
     row - .5, col - .5, row + .5, col + .5, 
     xlim = c(0, xp * nr), ylim = c(0, xp * nc),
     xlab = "", ylab = ""))

ここに画像の説明を入力

10x10 の緑/赤に改訂され、男のアイコンを使用してバージョンが追加されました。

于 2014-12-10T16:55:48.397 に答える
3

私もこれ用のパッケージを作成しました (ほぼ同時に、他の男と一緒に :-)。geom_tile、geom_text (アイコンには FontAwesome などを使用)、および geom_point を使用する 3 つのアプローチがあります。後者の 2 つはドロップ シャドウを作成できますが、設定をいじる必要がある場合があります。ここにいくつかの例があります。

devtools::install_github("rubenarslan/formr")
library(formr)
qplot_waffle(rep(1:3,each=40,length.out=90))
library(ggplot2)
qplot_waffle_text(rep(1, each = 30), symbol = "", rows = 3) + ggtitle("Number of travellers in 2008")

単純なカウント

旅行者数

于 2015-03-24T17:28:41.753 に答える
2

OPggplot2は ggplot2 Google グループで解決策を要求したので、ここにも投稿します。

ggplot(DF, aes(row, col, fill=value)) + 
  geom_tile(colour="white", lwd=2) + 
  scale_fill_manual(values=c("green","red")) +
  theme(panel.background=element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank()) +
  guides(fill=FALSE)

ここに画像の説明を入力

于 2015-01-22T20:18:17.303 に答える