我正在创建一个绘制图形的条形图,并根据我的数据集更改的过滤器。
数据集如下所示:
tabNew <- structure(list(Group = c("2016-11", "2016-12", "2017-01", "2017-
02", "2017-03"),
`Did Not Meet Expectations` = c(3, 0.8, 1.5, 0.8, 1.7),
`Exceeded Expectations` = c(45, 50.6, 32.3, 49.5, 55.6),
`Met Expectations` = c(51.2, 48.5, 66.2, 49.5, 42.4),
Unacceptable = c(0.7, 0, 0, 0.1, 0.2)),
.Names = c("Group", "Did Not Meet Expectations",
"Exceeded Expectations", "Met Expectations", "Unacceptable"),
row.names = c(NA, -5L), class = "data.frame")
绘制图表的代码如下:
x <- list(
title = "Time"
)
y <- list(
title = "Percent"
)
p <- plot_ly(tabNew, x = ~Group, y = ~`Unacceptable`, colors = c("red", "yellow", "green", "blue"),
name = 'Unacceptable', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~`Did Not Meet Expectations`, name = 'Did Not Meet Expectations') %>%
add_trace(y = ~`Met Expectations`, name = 'Met Expectations') %>%
add_trace(y = ~`Exceeded Expectations`, name = 'Exceeded Expectations') %>%
layout(xaxis = x, yaxis = y)
图表如下:
此数据集是Group
代表Months
的示例。有时基于过滤器,Group
可以代表Quarters
,在这种情况下,可能不会有所有其他列。因此,我们有可能只有Did Not Meet Expectations
,Exceeded Expectations
和Met Expectations
。
在任何一种情况下,我都不想要默认颜色。我希望Unacceptable
Red
显示Did Not Meet Expectations
,Yellow
如果可以显示为Met Expectations
,同样Blue
显示Exceeded Expectations
Green
为$column = $_POST['column'];
$rowName = $_POST['rowName'];
$qry = $connection->prepare("SELECT * FROM database_name WHERE row_name=?");
$qry->bind_param("s",$rowName);
$qry->execute();
$result = $qry->get_result();
$value = $result[$column];
。有没有办法指定这个顺序?
答案 0 :(得分:2)
require(plotly)
df <- data.frame(
Group = c("2016-11", "2016-12", "2017-01", "2017-02", "2017-03"),
DidNot = c(3, 0.8, 1.5, 0.8, 1.7),
Exceeded = c(45, 50.6, 32.3, 49.5, 55.6),
Met = c(0.7, 0, 0, 0.1, 0.2),
Unacceptable = c(0.7, 0, 0, 0.1, 0.2)
)
plot_ly(df, x = ~Group) %>%
add_trace(y = ~Exceeded,
name = "Exceeded Expectations",
type = "scatter",
mode = "lines",
line = list(color = "green")) %>%
add_trace(y = ~Unacceptable,
name = "Unacceptable",
type = "scatter",
mode = "lines",
line = list(color = "red")) %>%
layout(xaxis = list(title = "Time"),
yaxis = list(title = "Percent"))
此代码生成:
我修改了你的数据帧语法以便我更整洁,我显然没有为你绘制所有的行,但是你得到了图片。
答案 1 :(得分:1)
p <- plot_ly(tabNew, x = ~Group, y = ~`Unacceptable`,
name = 'Unacceptable', type = 'scatter', mode = 'lines',
line=list(color="red")) %>%
add_trace(y = ~`Did Not Meet Expectations`, name = 'Did Not Meet Expectations',
line=list(color="yellow")) %>%
add_trace(y = ~`Met Expectations`, name = 'Met Expectations',
line=list(color="green")) %>%
add_trace(y = ~`Exceeded Expectations`, name = 'Exceeded Expectations',line=list(color="blue")) %>%
layout(xaxis = x, yaxis = y)
如果您使用的是虹膜数据集
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, , colors=c("Red","Green","Blue"))
答案 2 :(得分:0)
另一种选择是以长格式融合data.frame并将标准名称映射为特定颜色(另请参见Scatter and Line Plots in R):
def printBackwards(list_1):
list_2 = []
i = len(list_1)
while (i > 0):
list_2.append(list_1[i-1])
i = i -1
return list_2
myslist = [2 , 4 , 6 , 8 , 10]
print(printBackwards(myslist))
当data.frame中没有所有列名称时,这也应该起作用。