在R

时间:2017-05-05 12:17:00

标签: r plotly

我在R(版本3.4)中使用plotly(版本4.6.0)来创建两条围绕它们的置信区间的行。传说没有显示。任何人都猜到了什么?

以下是情节: Output from plotly without legend

似乎忽略了图例切换。填充(置信区间)为假,主图为真。将它们全部变为true会给出六个图例条目,但我只想要两个。

以下是代码:

plot_ly(x = ~observed$time, y = ~observed$interval_upper,
    type = 'scatter',
    mode = 'lines',
    line = list(color = 'transparent'),
    showlegend = FALSE,
    name = 'Upper bound')

    %>% add_trace(x = ~observed$time, y = ~observed$interval_lower,
    type = 'scatter',
    mode = 'lines',
    fill = 'tonexty',
    fillcolor='rgba(255,127,14,0.2)',
    line = list(color = 'transparent'),
    showlegend = FALSE,
    name = 'Lower bound')

    %>% add_trace(x = ~observed$time, y = ~observed$observed_power,
    type = 'scatter',
    mode = 'lines',
    line = list(color='rgb(255,127,14)'),
    showlegend = TRUE,
    name = 'Observed')

    %>% add_trace(x = ~forecast$time, y = ~forecast$interval_upper,
    type = 'scatter',
    mode = 'lines',
    line = list(color = 'transparent'),
    showlegend = FALSE,
    name = 'Upper bound')

    %>% add_trace(x = ~forecast$time, y = ~forecast$interval_lower,
    type = 'scatter',
    mode = 'lines',
    fill = 'tonexty',
    fillcolor='rgba(31,119,180,0.2)',
    line = list(color = 'transparent'),
    showlegend = FALSE,
    name = 'Lower bound')

    %>% add_trace(x = ~forecast$time, y = ~forecast$baseline_power,
    type = 'scatter',
    mode = 'lines',
    line = list(color='rgb(31,119,180)'),
    showlegend = TRUE,
    name = 'Forecast')

    %>% layout(legend = list(x = 0.80, y = 0.90))

1 个答案:

答案 0 :(得分:1)

plot_ly中的第一个showlegend应该始终为TRUE,否则会掩盖其他的,尝试交换跟踪。

这个例子取自plotly网站显示的问题 (https://plot.ly/r/legend/

CREATE TABLE #Checklist
(
	Checklist varchar(200),
	Notes varchar(50)
)


SET NOCOUNT ON;  

DECLARE
 @HeaderName varchar(200),
 @GroupName varchar(200),
 @TaskGroupName varchar(200),
 @TaskDesc nvarchar(200)

DECLARE header_cursor CURSOR FOR   
SELECT DISTINCT tblCheckListHdr.CLHName, tblCheckListDtl.CLDGroupName
	FROM tblCheckListHdr
	INNER JOIN tblCheckListDtl ON tblCheckListHdr.CLHId = tblCheckListDtl.CLHId
	INNER JOIN tblServiceType ON tblCheckListHdr.CLHId = tblServiceType.SelectedCLHId
	INNER JOIN tblWorkOrder ON tblServiceType.ServiceTypeId = tblWorkOrder.ServiceType
WHERE tblWorkOrder.WorkOrderId = @WOid
	AND tblCheckListHdr.CLHActive = 1
ORDER BY tblCheckListDtl.CLDGroupName

OPEN header_cursor 

FETCH NEXT FROM header_cursor INTO @HeaderName, @GroupName

WHILE @@FETCH_STATUS = 0  
	BEGIN  

	INSERT INTO #Checklist (Checklist, Notes)
	SELECT @HeaderName + ' | ' + @GroupName, 'Y, N, NA, Notes' AS Notes

		DECLARE detail_cursor CURSOR FOR   
		SELECT tblCheckListDtl.CLDGroupName
			, tblCheckListDtl.CLDTaskDesc
		FROM tblCheckListHdr
		INNER JOIN tblCheckListDtl ON tblCheckListHdr.CLHId = tblCheckListDtl.CLHId
		INNER JOIN tblServiceType ON tblCheckListHdr.CLHId = tblServiceType.SelectedCLHId
		INNER JOIN tblWorkOrder ON tblServiceType.ServiceTypeId = tblWorkOrder.ServiceType
		WHERE tblWorkOrder.WorkOrderId = @WOid
			AND tblCheckListHdr.CLHActive = 1
			AND tblCheckListDtl.CLDActive = 1
			AND tblCheckListDtl.CLDGroupName = @GroupName
		ORDER BY tblCheckListDtl.CLDDisplayOrder

		OPEN detail_cursor  
		FETCH NEXT FROM detail_cursor INTO @TaskGroupName, @TaskDesc

		WHILE @@FETCH_STATUS = 0  
		BEGIN  

				INSERT INTO #Checklist (Checklist)
				SELECT @TaskDesc

			FETCH NEXT FROM detail_cursor INTO @TaskGroupName, @TaskDesc

		END

		CLOSE detail_cursor  
		DEALLOCATE detail_cursor  
		FETCH NEXT FROM header_cursor INTO @HeaderName, @GroupName
	END   
CLOSE header_cursor;  
DEALLOCATE header_cursor

SELECT #Checklist.Checklist, #Checklist.Notes
FROM #Checklist