我需要使用dplyr转换数据才能进行一些绘图
我需要创建一个新列,根据它所属的因素说明行的百分比。
基本上我需要用百分比来绘制这个图,所以所有列都是相同的高度,按价格范围显示每个电视品牌的百分比。
最后我想实现这个目标(基于我的数据):
由此:
第1步:
分组" rangos"," marca"。然后创建一个新的colum" cantidad.marca"每个价格范围内的品牌长度。
最后,我想制作一个专栏"百分比"这可以通过"范围(rango)"计算出的总数量。并划分每个品牌"在该范围内的数字。
我已使用此代码,以及此变体porcentaje = cantidad.marca/sum(s.tvs.porcentajes$cantidad.marca)
s.tvs.porcentajes <- s.tvs %>%
group_by(rangos, marca) %>%
summarise(cantidad.marca = length(marca),
porcentaje = cantidad.marca/sum(cantidad.marca))
另一种方法:
我想知道每个范围内的电视品牌总数,并使用这些数字来划分其他数据框中的出现次数。
s.tvs.p <- s.tvs %>%
group_by(rangos) %>%
summarise(totales.rango = length(rangos))
但我不知道如何将它们结合起来。
任何提示?
这是s.tvs
:
您可以使用以下代码重现它:
s.tvs <- structure(list(marca = structure(c(3L, 2L, 3L, 2L, 2L, 1L, 5L,
1L, 2L, 1L, 1L, 5L, 2L, 1L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 3L, 1L,
3L, 3L, 2L, 1L, 3L, 2L, 4L, 3L, 2L, 1L, 3L, 4L, 2L, 2L, 3L, 1L,
1L, 3L, 2L, 3L, 3L, 2L, 1L, 2L, 2L, 3L, 1L, 3L, 4L, 1L, 2L, 3L,
1L, 3L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 4L, 2L, 3L, 2L, 1L, 3L, 2L,
1L, 2L, 2L, 3L, 3L, 3L, 4L, 2L, 3L, 1L, 1L, 1L, 5L, 1L, 1L, 3L,
4L, 2L, 6L, 2L, 1L, 3L, 1L, 3L, 3L, 2L, 1L, 2L, 2L, 5L, 3L, 1L,
2L, 2L, 3L, 3L, 1L, 1L, 4L, 6L, 1L, 4L, 3L, 2L, 6L, 3L, 1L, 1L,
4L, 1L, 5L, 3L, 4L, 1L, 1L, 2L, 4L, 1L, 5L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 3L, 3L, 1L, 1L, 3L, 1L, 1L, 3L, 2L, 2L, 3L, 3L, 2L), .Label = c("LG",
"SAMSUNG", "SONY", "HISENSE", "PANASONIC", "AOC"), class = c("ordered",
"factor")), producto = c("LED 55\" XBR-55X855B", "LED 65\" 65JU7500",
"LED 49\" XBR-49X855B", "LED 55\" 55JS9000", "LED 65\" 65JS9000",
"LED 32\" 32LF595B", "LED 32\" TC-32AS600L", "LED 79\" 79UG8800",
"LED 48\" 48JU6700 ULT...", "LED 49\" 49LF5900 WEB...", "LED 55\" 55LF6500",
"LED 42\" TC-42AS610L", "LED 40\" 40J5500", "LED 49\" 49LF6350",
"LED 65\" XBR-65X855C", "LED 50\" KDL-50FA95C", "LED 79\" 79UG8800",
"LED 55\" KDL55W805C", "LED 49\" 49LF6350", "LED 49\" ULTRA HD 4K ...",
"LED 84\" 84UB9800", "LED 55\" KDL-55W805B", "LED 55\" 55LF6350",
"LED 65\" KDL65W855C", "LED 50\" KDL-50W805B", "LED 48\" 48J5500",
"LED 65\" 65UG8700 WE...", "LED 50\" KDL50W805C", "LED 55\" JU6700",
"LED 50\" SMART ULTRA ...", "LED 50\" KDL50W805C", "LED 55\" JU6700",
"LED 55\" 55UG8700 WEB...", "LED 55\" XBR-55X855C", "LED 48\" LTDN48K390XW...",
"LED 50\" 50J5500", "LED 48\" 48J5500", "LED 55\" KDL55W805C",
"LED 55\" 55LB7200", "LED 60\" 60LB5800", "LED 65\" XBR-65X855C",
"LED 60\" 60JU6500 + H...", "LED 50\" KDL-50FA95C", "LED 60\" KDL-60W605",
"LED 40\" 40J5300", "LED 49\" 49UF8500", "LED 40\" 40J5500",
"LED 40\" 40J6400", "LED 55\" KDL-55W805B", "LED 49\" 49LF6400",
"LED 55\" XBR-55X905C", "LED 40\" LEDN40K360PV...", "LED 65\" 65UG8700 WE...",
"LED 48\" 48J5300", "LED 50\" KDL50W805C", "LED 55\" 55UG8700 WEB...",
"LED 75\" XBR-75X945C", "LED 49\" 49LF5900 WEB...", "LED 65\" 65UF8500",
"LED 40\" UN40J6500 SM...", "LED 49\" 49LF5900 WEBOS LITE", "LED 40\" UN40J6500 SMART TV",
"LED 55\" 55UF9500", "LED 49\" 49LF6350", "LED 32\" HD 2HDMI 2US...",
"LED 65\" 65JU7500", "LED 65\" XBR-65X905C", "LED 32\" 32J4000",
"LED 70\" ULTRA HD 4K ...", "LED 50\" KDL-50FA95C", "SMART TV 50\" UN50JS7...",
"LED 55\" 55UF8500", "LED 55\" 55J6500", "LED 48\" 48J5500",
"LED 48\" KDL-48R555C", "LED 40\" KDL-40R354B", "LED 60\" KDL-60W605",
"LED 48\" LTDN48K20DAM", "LED 55\" 55JU6500", "LED 55\" XBR-55X855C",
"LED 43\" 43LF6350", "LED 49\" 49LF6450", "LED 55\" 55UF6800 WEB...",
"LED 32\" TC-32AS600L", "OLED 55\" 55EC9300", "LED 65\" 65UF9500",
"LED 55\" XBR-55X855B", "LED 40\" FULL HD 40D5...", "LED 48\" UN48J6400",
"LED 40\" LE40F1551 FU...", "LED 32\" 32J4300", "LED 49\" LF5400",
"LED 49\" XBR-49X855B", "LED 55\" ULTRA HD 4K ...", "LED 32\" KDL-32R304B",
"LED 55\" KDL55W805C", "SMART TV 55\" UN55JS7...", "LED 32\" 32LF595B",
"LED 48\" 48JU6700", "LED 32\" JH4005", "LED 40\" TC-L40SV7L",
"LED 65\" KDL65W855C", "LED 55\" 55LF6350", "LED 50\" 50JU6500",
"LED 48\" 48J6500", "LED 40\" KDL-40R555C", "LED 32\" KDL-32R505C",
"LED 49\" 49UF6400", "LED 43\" 43UF6750", "LED 55\" SMART FULL H...",
"LED 58\" LE58F1552 FU...", "LED 84\" 84UB9800", "LED 48\" LTDN48K390XW...",
"LED 49\" XBR-49X835C", "LED 48\" 48JU6700 ULT...", "LED 32\" LE32W454F",
"LED 50\" KDL-50W805B", "LED 43\" 43UF6400", "LED 58\" 58UF8300 WEB...",
"LED 32\" SMART HD 32K...", "LED 43\" HD 43LF5410", "LED 42\" TC-42AS610L",
"LED 70\" KDL-70W855B", "LED 40\" SMART FULL H...", "LED 79\" 79UG8800",
"LED 32\" 32LF585B", "LED 48\" UN48JU6000", "LED 50\" SMART ULTRA ...",
"LED 32\" 32LF550B", "LED 32\" TC-32A400L", "LED 55\" 55J5500 ",
"LED 48\" 48JU6700", "LED 55\" 55J6500", "OLED 55\" 55EC9300",
"LED 65\" 65JU7500", "LED 48\" 48JU6700 ULT...", "LED 55\" 55UF6800 WEB...",
"LED 49\" XBR-49X855B", "LED 65\" XBR-65X905C", "LED 43\" 43UF6750",
"LED 84\" 84UB9800", "LED 55\" XBR-55X905C", "LED 58\" 58UF8300 WEB...",
"LED 79\" 79UG8800", "LED 55\" XBR-55X855B", "LED 65\" 65JU7500",
"LED 48\" 48JU6700", "LED 55\" XBR-55X855C", "LED 75\" XBR-75X945C",
"LED 48\" UN48JU6000"), precio.nuevo = c(4999L, 9199L, 3999L,
7999L, 11999L, 1299L, 1199L, 16999L, 2999L, 1699L, 3399L, 1899L,
1599L, 2099L, 8999L, 3999L, 19599L, 4299L, 1899L, 2899L, 17999L,
3499L, 2499L, 5999L, 2699L, 2199L, 8999L, 3499L, 3999L, 2599L,
3499L, 4499L, 5999L, 5999L, 1899L, 1999L, 1999L, 4299L, 3599L,
3499L, 8999L, 5999L, 4299L, 3699L, 1399L, 2999L, 1399L, 1699L,
3299L, 2599L, 7999L, 849L, 6999L, 1799L, 3499L, 5499L, 23999L,
1599L, 7399L, 1599L, 1599L, 1599L, 4549L, 1699L, 799L, 9199L,
11999L, 899L, 8499L, 3999L, 4599L, 3799L, 3999L, 1799L, 2199L,
1299L, 3699L, 1199L, 3499L, 5999L, 1399L, 2499L, 3999L, 1199L,
7999L, 6649L, 4999L, 849L, 2299L, 999L, 999L, 1399L, 3999L, 3699L,
949L, 4299L, 5699L, 1299L, 2999L, 899L, 1499L, 5999L, 2499L,
2799L, 2999L, 1599L, 1199L, 2399L, 1699L, 2499L, 2399L, 19999L,
1399L, 3999L, 2849L, 799L, 2699L, 1899L, 4499L, 999L, 1399L,
1899L, 6499L, 1099L, 15999L, 999L, 2599L, 2249L, 899L, 899L,
3299L, 2999L, 3999L, 7999L, 9199L, 2849L, 3999L, 3999L, 11999L,
1699L, 19999L, 7999L, 4499L, 15999L, 4999L, 9199L, 2999L, 5999L,
23999L, 2599L), categoria = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("SMART TV", "TODO LED", "TV-LED-CURVO", "ULTRA-HD-4K"
), class = "factor"), rangos = structure(c(5L, 5L, 4L, 5L, 5L,
1L, 1L, 5L, 3L, 2L, 3L, 2L, 2L, 2L, 5L, 4L, 5L, 4L, 2L, 3L, 5L,
3L, 2L, 5L, 3L, 2L, 5L, 3L, 4L, 3L, 3L, 4L, 5L, 5L, 2L, 2L, 2L,
4L, 4L, 3L, 5L, 5L, 4L, 4L, 1L, 3L, 1L, 2L, 3L, 3L, 5L, 1L, 5L,
2L, 3L, 5L, 5L, 2L, 5L, 2L, 2L, 2L, 5L, 2L, 1L, 5L, 5L, 1L, 5L,
4L, 5L, 4L, 4L, 2L, 2L, 1L, 4L, 1L, 3L, 5L, 1L, 2L, 4L, 1L, 5L,
5L, 5L, 1L, 2L, 1L, 1L, 1L, 4L, 4L, 1L, 4L, 5L, 1L, 3L, 1L, 1L,
5L, 2L, 3L, 3L, 2L, 1L, 2L, 2L, 2L, 2L, 5L, 1L, 4L, 3L, 1L, 3L,
2L, 4L, 1L, 1L, 2L, 5L, 1L, 5L, 1L, 3L, 2L, 1L, 1L, 3L, 3L, 4L,
5L, 5L, 3L, 4L, 4L, 5L, 2L, 5L, 5L, 4L, 5L, 5L, 5L, 3L, 5L, 5L,
3L), .Label = c("ENTRE S/.500 Y S/.1500", "ENTRE S/.1500 Y S/.2500",
"ENTRE S/.2500 Y S/.3500", "ENTRE S/.3500 Y S/.4500", "MAYOR A S/.4,500"
), class = c("ordered", "factor"))), .Names = c("marca", "producto",
"precio.nuevo", "categoria", "rangos"), row.names = c(NA, 150L
), class = "data.frame")
答案 0 :(得分:3)
对于问题的第一部分,您可以获得百分比列(porcentaje)。
ring_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent_for_ring_button = new Intent();
intent_for_ring_button.setAction(Intent.ACTION_GET_CONTENT);
File file = new File(REPORTS_DIRECTORY);
intent_for_ring_button.setDataAndType(Uri.fromFile(file),"application/file");
startActivityForResult(Intent.createChooser(intent_for_ring_button,"Open folder"), 0);
}
});
输出:
library(dplyr)
s.tvs.porcentajes <- s.tvs %>%
group_by(rangos, marca) %>%
summarise(cantidad.marca = length(marca)) %>%
mutate( porcentaje = cantidad.marca/sum(cantidad.marca))
绘制图表:
Source: local data frame [22 x 4]
Groups: rangos [5]
rangos marca cantidad.marca porcentaje
(fctr) (fctr) (int) (dbl)
1 ENTRE S/.500 Y S/.1500 LG 7 0.25000000
2 ENTRE S/.500 Y S/.1500 SAMSUNG 5 0.17857143
3 ENTRE S/.500 Y S/.1500 SONY 3 0.10714286
4 ENTRE S/.500 Y S/.1500 HISENSE 7 0.25000000
5 ENTRE S/.500 Y S/.1500 PANASONIC 4 0.14285714
6 ENTRE S/.500 Y S/.1500 AOC 2 0.07142857
7 ENTRE S/.1500 Y S/.2500 LG 13 0.41935484
8 ENTRE S/.1500 Y S/.2500 SAMSUNG 10 0.32258065
9 ENTRE S/.1500 Y S/.2500 SONY 2 0.06451613
10 ENTRE S/.1500 Y S/.2500 HISENSE 3 0.09677419
.. ... ... ... ...
如果你想要y轴的百分比:
library(ggplot2)
plt <- ggplot(s.tvs.porcentajes, aes(x=rangos, y= porcentaje ,fill=marca)) +
geom_bar(stat = "identity", width = .7)
答案 1 :(得分:1)
library(rCharts)
n1 <- nPlot(porcentaje ~ rangos, group = marca, data= s.tvs.porcentajes, type = 'multiBarChart')
n1
n1$save("tvbrand.html") # saving as html page
n1$publish("NCR", host = 'gist') #posting on webpage